shell腳本中也可以實現邏輯判斷。
案例4:shell腳本中的邏輯判斷
如果你學過C或者其他語言,相信你不會對if 陌生,在shell腳本中我們同樣可以使用if邏輯判斷。在shell中if判斷的基本語法為:
1)不帶else
if 判斷語句; then
command
fi
#! /bin/bash
## author:Xiong Xuehao
## Use if in this script. read -p "Please input a number: " a if ((a<60));then echo "you didn't pass this test" fi
在if1.sh中出現了 ((a<60))這樣的形式,這是shell腳本中特有的格式,用一個小括號或者不用都會報錯,請記住這個格式,即可。執行結果為:

2)帶有else
if 判斷語句 ; then
command
else
command
fi
#! /bin/bash
## author:Xiong Xuehao
## Use if in this script. read -p "Please input a number: " a if ((a<60));then echo "you didn't pass this test" else echo "you pass this test" fi
執行結果為:

3)帶有elif
if 判斷語句一 ; then
command
elif 判斷語句二; then
command
else
command
fi
#! /bin/bash
## author:Xiong Xuehao
## Use if in this script. read -p "Please input a number: " a if ((a<60));then echo "you didn't pass this test" elif ((a>=60)) && ((a<85));then echo "you pass this test" else echo "Verry Good!" fi
這里的 && 表示“並且”的意思,當然你也可以使用 || 表示“或者”,執行結果:

以上只是簡單的介紹了if語句的結構。在判斷數值大小除了可以用”(( ))”的形式外,還可以使用”[ ]” 注意方括號里面需要有空格。但是就不能使用>, < , = 這樣的符號了,要使用 -lt (小於),-gt (大於),-le (小於等於),-ge (大於等於),-eq (等於),-ne (不等於)。
#! /bin/bash
## author:Xiong Xuehao
## Use if in this script. read -p "Please input a number: " a if [ $a -lt 60 ];then echo "$a lt than 60"; elif [ $a -ge 60 ] && [ $a -lt 85 ];then echo "you pass this test" elif [ $a -eq 100 ];then echo "$a eq than 100. Very Good!" elif [ $a -ne 0 ];then echo "$a ne than 100. Your Input error!" elif [ $a -gt 100 ];then echo "$a gt than 100. Your Input error!" else echo "Your Input error!" fi
運行結果如圖:

案例5:shell腳本中判斷檔案屬性
shell 腳本中if還經常判斷關於檔案屬性,比如判斷是普通文件還是目錄,判斷文件是否有讀寫執行權限等。常用的也就幾個選項:
-e :判斷文件或目錄是否存在
-d :判斷是不是目錄,並是否存在
-f :判斷是否是普通文件,並存在
-r :判斷文檔是否有讀權限
-w :判斷是否有寫權限
-x :判斷是否可執行
使用if判斷時,具體格式為: if [ -e filename ] ; then
#! /bin/bash
## author:Xiong Xuehao
## File properties.
read -p "Please input a file or directory: " a ###判斷文件或目錄是否存在 if [ -e $a ];then echo "$a The file or directory already exists." else echo "$a The file or directory does not exist!" fi ###判斷文件或目錄 if [ -d $a ];then echo "$a The path is a directory and already exists." elif [ -f $a ];then echo "$a The path is a file and already exists." else echo "$a I don't know!" fi ###判斷權限 if [ -r $a ];then echo "$a The document has read permission." else echo "$a The document does not have read permission!" fi if [ -w $a ];then echo "$a The document has write permission." else echo "$a The document does not have write permission!" fi if [ -x $a ];then echo "$a The document has execution permission." else echo "$a The document does not have execution permission!" fi
執行如圖:

案例6:shell腳本中用case邏輯判斷
在shell 腳本中,除了用if來判斷邏輯外,還有一種常用的方式,那就是case了。具體格式為:
case 變量 in
value1)
command
;;
value2)
command
;;
value3)
command
;;
*)
command
;;
esac
上面的結構中,不限制value的個數,*則代表除了上面的value外的其他值。下面筆者寫一個判斷輸入數值是奇數或者偶數的腳本。
#! /bin/bash
## author:Xiong Xuehao
## Use case in this script. read -p "Please input a number: " n ###判斷奇數或者偶數 a=$[$n % 2] case $a in 1) echo "The remainder is $a. $n The num is odd." ;; 0) echo "The remainder is $a. $n The num is even." ;; esac
輸入任意一個自然數,除以2得余數 $a 的值或為1或為0,執行結果為:

case腳本常用於編寫系統服務的啟動腳本,例如/etc/init.d/iptables中就用到了,不妨去查看一下。
