1、撰寫一個script,讓使用者輸入:1.first name 2.last name,最後在屏幕上顯示:Your full name is:的內容bash
1
2
3
4
|
#!/bin/bash
read -p
"Please input your firstname:"
firstname
read -p
"Please input your lastname:"
lastname
echo -e
"Your full name is:$firstname $lastname"
|
2、用戶輸入2個變量,而後將2個變量相乘,最後輸出相乘結果this
1
2
3
4
5
|
#!/bin/bash
read
-p
"input first number:"
firstnu
read
-p
"input second number:"
secnu
total=$(($firstnu*$secnu))
echo
-e
"the result of $firstnu x $secnu is $total"
|
3、使用source執行script,可將變量置於父進程(環境)中spa
4、用戶輸入一個filename,並作以下判斷:code
filename的檔案是否存在,不存在就終止程序進程
若存在,則判斷是文件仍是目錄,並輸入結果ip
判斷當前身份用戶對該檔案/目錄所具備的權限,並輸出結果ci
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#!/bin/bash
echo
-e
"Please input a filename,this program will check the file's type and permission"
read
-p
"Input a filename:"
filename
#1.判斷使用者是否真有輸入字符串
test
-z $filename&&
echo
"You must input a filename"
&&
exit
0
#2.判斷檔案是否存在,不存在則終止程序
test
! -e $filename&&
echo
"the filename $filename no exist"
&&
exit
0
#3.判斷文件類型及屬性
test
-f $filename&&filetype=
"regular file"
test
-d $filename&&filetype=
"directory"
test
-r $filename&&perm=
"readable"
test
-w $filename&&perm=
"$perm writable"
test
-x $filename&&perm=
"$perm executable"
#4.開始輸出信息
echo
"The filename: $filename is a $filetype"
echo
"The permisson are: $perm"
|
5、使用中括號代替test進行判斷字符串
1
2
3
4
5
|
#!/bin/bash
read
-p
"Please choose Y/N:"
yn
[
"$yn"
==
"y"
-o
"$yn"
==
"Y"
]&&
echo
-e
"OK,continue"
&&
exit
0
[
"$yn"
==
"n"
-o
"$yn"
==
"N"
]&&
echo
-e
"Oh,interrupt"
&&
exit
0
echo
-e
"I don't know what you mean"
&&
exit
0
|
6、使用if..elif..then..fi判斷式input
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#!/bin/bash
read
-p
"Please choose Y/N:"
yn
if
[
"$yn"
==
"Y"
]||[
"$yn"
==
"y"
];
then
echo
-e
"OK,continue"
elif
[
"$yn"
==
"N"
]||[
"$yn"
==
"n"
];
then
echo
-e
"Oh,interrupt"
else
echo
-e
"I don't know what you mean"
fi
|