1. if-then語句
shell
1. if-then語句express
格式:bash
if COMMAND ide
then
測試
COMMAND
this
filua
說明:當if 後的shell命令運行結果返回0值時,則then後的命令會執行,反之則不會執行ci
示例:字符串
[root@localhost ~]# vi test.sh #!/bin/bash #Testing if-then if pwd then echo "It worked" fi ~ ~ ~ "test.sh" 6L, 62C written [root@localhost ~]# ./test -bash: ./test: No such file or directory [root@localhost ~]# ./test.sh /root It worked
if後的pwd正常執行後返回0值,then後的語句繼續執行
get
示例:
[root@localhost ~]# vi test.sh #!/bin/bash #Testing a bad command if BadCommand then echo "It worked" fi echo "We are outside the if statment" ~ ~ ~ "test.sh" 7L, 113C written [root@localhost ~]# ./test.sh ./test.sh: line 3: BadCommand: command not found We are outside the if statment
if 後的命令執行失敗,then後的語句也未執行,可是if-then後的語句執行了。
2. if-then-else語句
格式:
if COMMAND
then
COMMAND
else
COMMAND
fi
說明:當if後的命令執行成功後會執行then下的命令,若是if後的命令沒執行成功則會執行else下的命令
示例:
[root@localhost ~]# vi test.sh #!/bin/bash #Testing the else section # testuser=NoSuchUser # if grep $testuser /etc/passwd then echo "The bash files for user $testuser are:" ls -a /home/$testuser/.b* echo else echo "The user $testuser does not exist on this system." echo fi ~ ~ ~ ~ "test.sh" 14L, 249C written [root@localhost ~]# ./test.sh The user NoSuchUser does not exist on this system.
3. 嵌套if
3.1 if-then能夠嵌套在else中,用於作條件判斷。
格式:
if COMMAND
then
COMMAND
else
COMMAND
if COMMAND
then
COMMAND
fi
fi
示例:
[root@localhost ~]# vi test.sh #!/bin/bash #Testing nested if testuser=NosuchUser # if grep $testuser /etc/passwd then echo "The user $testuser is exist on this system" else echo "The user $testuser does not on this system" if ls /home/$testuser then echo "/home/$testuser is a directory" fi fi ~ ~ ~ ~ ~ "test.sh" 14L, 271C written [root@localhost ~]# ./test.sh The user NosuchUser does not on this system /home/NosuchUser is a directory
3.2 if-then-elif-then-elif-then...[else-]fi:能夠增長多個判斷條件並且比if嵌套的方式邏輯更加清晰
格式:
if
then
elif
then
...
...
else #可根據實際條件用或不用。
fi
示例:
[root@localhost ~]# vi test.sh #!/bin/bash #Testing if-then-elif-then-else-fi # file1=/home/NosuchUser if [ -f $file1 ] then echo "$file1 is a file" elif [ -d $file1 ] then echo "$file1 is a directory" else echo "$file1 isn\'t exist" fi ~ ~ ~ ~ ~ "test.sh" 13L, 210C written [root@localhost ~]# ./test.sh /home/NosuchUser is a directory
3.3 test語句
格式:test condition
在if-then-fi中的格式爲:
if test condition
then
COMMAND
fi
說明:test主要用於在腳本中進行條件測試,也能夠在腳本中用於測試命令,當命令成功執行或判斷條件爲ture時則test會返回0值,而後then下的命令繼續執行。
[root@localhost ~]# vi test.sh #!/bin/bash #Testing test condition # if test then echo "It\'t ture" else echo "It\'t false" fi ~ ~ ~ ~ ~ "test.sh" 9L, 98C written [root@localhost ~]# ./test.sh It\'t false
上述可看出test後爲空,返回的值非0,則執行了else下面的命令。
test命令還能夠檢測變量值是否爲空。若不爲空則返回0值執行then下的COMMADN。
[root@localhost ~]# vi test.sh #!/bin/bash #Testing test condition # variable=full # if test $variable then echo "$variable is exist" else echo "$variable is not exist" fi ~ ~ ~ [root@localhost ~]# ./test.sh full is exist
test命令還能夠使用 [ condition ] 或` condition `來實現,方括號內先後都必須有空格,不然沒法正常執行。
test命令可執行如下三種判斷條件:數值比較、字符串比較、文件比較
3.3.1 數值比較
數值比較表達式
n1 -gt n2 :n1是否大於n2
n1 -ge n2 :n1是否大於等於n2
n1 -eq n2 :n1是否等於n2
n1 -le n2 :n1是否小於等於n2
n1 -lt n2 :n1是否小於n2
n1 -eq n2 :n1是否不等於n2
[root@localhost ~]# vi test.sh #!/bin/bash #Testing $var1 numeric test evaluations # var1=10 var2=4 # if [ $var1 -gt 5 ] then echo "Value $var1 is greater than 5" else echo "Value $var1 is not greater than 5" fi # if [ $var1 -eq $var2 ] then echo "Value $var1 is equal to $var2 " else echo "Value $var1 is not equal to $var2 " fi ~ ~ ~ ~ "test.sh" 20L, 305C written [root@localhost ~]# ./test.sh Value 10 is greater than 5 Value 10 is not equal to 4
test命令沒法測試浮點數。
3.3.2 字符串比較
字符串比較表達式
str1 = str2 :str1等於str2
str1 != str2 :str1不等於str2
str1 < str2 :str1小於str2
str1 > str2 :str1大於str2
-n str1 :str1長度是否不爲0
-z str1:str1長度是否爲0
字符串比較時還應注意如下幾點:
(1)字符串比較會比較全部字符的特徵,例如字符大小寫、標點符號等;
(2)在使用大於號和小於號時必須轉義
(3)比較測試中大寫字符被認爲是小於小寫字母的
(4)-n和-z是用來判斷變量是否包含字符串的。
3.3.3 文件比較
文件比較表達式以下:
-d filename :檢查文件是否存在且爲一個目錄
-f filename :檢查文件是否存在且爲一個文件
-e filename:檢查文件是否存在
-r :檢查文件是否可讀
-s:檢查文件是否非空
-w:檢查文件是否可寫
-x:檢查文件是否可執行
-O:檢查執行用戶是否爲文件的屬主
-G:檢查執行用戶是否爲文件的屬組
file1 -nt file2 :檢查file1是否比file2新,一般以文件建立時間來比較
file1 -ot file2 :檢查file是否比file2舊,一般以建立時間來比較
4. 複合條件測試
if [ condition1 ] || [ condition2 ] then COMMAND fi
當[ condition1 ] 和 [ condition2 ] 任一爲執行結果爲真時會執行then下的COMMAND
if [ condition1 ] && [ condition2 ] then COMMAND fi
當[ condition1 ] 和 [ condition2 ]執行結果都爲真時纔會執行then下的COMMAND
5. if-then的高級特性
5.1 (( expression )):test還支持(( ))的這種用法
會用到的運算表達式除了test所支持的,還有如下幾種:
var++:後增
var-- :後減
++var :先增
--var :先減
!:邏輯求反
~:位運算
**:冪運算
<<:左位移
>>:右位移
&:位布爾和
|:位布爾或
&&:邏輯和
||:邏輯或
雙括號特性:
(1)大於號與小於號無需轉義
(2)支持更多的運算表達式
(( $var ** 2 >90 ))
5.2 ` expression `:test支持[[]]模式
雙方括號特性:支持模式匹配
[[ $USER == r* ]]
6. case命令
說明:case能夠將if-then-elif-then-else-fi的模式簡化,使用表格形式檢查單個變量的多個值。
格式:
case VARIABLE in
PATTERN1 | PATTER2 ) COMMANDS1;;
PATTERN3 ) COMMANDS2;;
*)DEFAULT COMMANDS3;;
esac
示例:
將以下if-then腳本改造程case模式
if-then模式
[root@localhost ~]# vi test.sh #!/bin/bash #Looking for a possible value # if [ $USER = "root" ] then echo "Welcome $USER" echo "Please enjoy your visit" elif [ $USER = "rich" ] then echo "Welcome $USER" echo "Please enjoy your visit" elif [ $USER = "jessica" ] then echo "Special testing accont" elif [ $USER = "barbara" ] then echo "Do not forget to logout when you\'re done" else echo "Sorry, you are not allowed here" fi ~ "test.sh" 20L, 401C written [root@localhost ~]# ./test.sh Welcome root Please enjoy your visit
case模式:
[root@localhost ~]# vi test.sh #!/bin/bash #Looking for case case $USER in "root" | "rich") echo "Welcome $USER" echo "Please enjoy your visit";; "jessica") echo "Special testing accont";; "barbara") echo "Do not forget to logout when you\'re done";; *) echo "Sorry ,you are not allowed here";; esac ~ ~ "test.sh" 12L, 273C written [root@localhost ~]# ./test.sh Welcome root Please enjoy your visit