1. 程式人生 > 其它 >學習linux的菜鳥 shell指令碼中的邏輯判斷

學習linux的菜鳥 shell指令碼中的邏輯判斷

shell指令碼中的邏輯判斷

if 邏輯判斷。在shell中if判斷的基本語法為:

1)不帶else

if  判斷語句; then
    command
fi

例如:

[root@localhost sbin]# cat if1.sh
#! /bin/bash

read -p "Please input your score: " a
if (($a<60)); then
    echo "You didn't pass the exam."
fi

在if1.sh中出現了 (($a<60)) 這樣的形式,這是shell指令碼中特有的格式,用一個小括號或者不用都會報錯,

請記住這個格式或者可以寫為[ $a lt 60 ]。執行結果為:

[root@localhost sbin]# sh if1.sh
Please input your score: 90
[root@localhost sbin]# sh if1.sh
Please input your score: 33
You didn't pass the exam.

2)帶有else

if  判斷語句  ; then
    command
else
    command
fi

例如:

[root@localhost sbin]# cat if2.sh
#! /bin/bash

read -p "Please input your score: " a
if (($a<60)); then
     echo "You didn't pass the exam."
else
     echo "Good! You passed the exam."
fi

執行結果:

[root@localhost sbin]# sh if2.sh
Please input your score: 80
Good! You passed the exam.
[root@localhost sbin]# sh if2.sh
Please input your score: 25
You didn't pass the exam.

和上一例唯一區別的地方是,如果輸入大於等於60的數字會有所提示。


3)帶有elif

if  判斷語句一  ; then
    command
elif  判斷語句二; then
    command
else
    command
fi

例如:

[root@localhost sbin]# cat if3.sh
#! /bin/bash

 read -p "Please input your score: " a
 if (($a<60)); then
         echo "You didn't pass the exam."
 elif (($a>=60)) && (($a<85)); then
         echo "Good! You pass the exam."
 else
         echo "very good! Your socre is very high!"
 fi

這裡的 && 表示 “並且” 的意思,當然也可以使用 || 表示 “或者” 執行結果為:

[root@localhost sbin]# sh if3.sh
Please input your score: 90
very good! Your socre is very high!
[root@localhost sbin]# sh if3.sh
Please input your score: 60
Good! You pass the exam.

以上只是簡單的介紹了if語句的結構。在判斷數值大小除了可以用 (( )) 的形式外,還可以使用 [ ] 但是就不能使用>, < , = 這樣的符號了,要使用 -lt (小於),-gt (大於),-le (小於等於),-ge (大於等於),-eq (等於),-ne (不等於)。

[root@localhost sbin]# a=10; if [ $a -lt 5 ]; then echo ok; fi
[root@localhost sbin]# a=10; if [ $a -gt 5 ]; then echo ok; fi
ok
[root@localhost sbin]# a=10; if [ $a -ge 10 ]; then echo ok; fi
ok
[root@localhost sbin]# a=10; if [ $a -eq 10 ]; then echo ok; fi
ok
[root@localhost sbin]# a=10; if [ $a -ne 10 ]; then echo ok; fi

再看看if中使用 && 和 ||的情況:

[root@localhost sbin]# a=10; if [ $a -lt 1 ] || [ $a -gt 5 ]; then echo ok; fi
ok
[root@localhost sbin]# a=10; if [ $a -gt 1 ] || [ $a -lt 10 ]; then echo ok; fi
ok

shell 指令碼中if還經常判斷關於檔案屬性,比如判斷是普通檔案還是目錄,判斷檔案是否有讀寫執行許可權等。常用的也就幾個選項:

-e :判斷檔案或目錄是否存在

-d :判斷是不是目錄,並是否存在

-f :判斷是否是普通檔案,並存在

-r :判斷文件是否有讀許可權

-w :判斷是否有寫許可權

-x :判斷是否可執行

使用if判斷時,具體格式為:

if[-efilename];then

例子:

[root@localhost sbin]# if [ -d /home/ ]; then echo ok; fi
ok
[root@localhost sbin]# if [ -f /home/ ]; then echo ok; fi

因為 /home/ 為目錄為非檔案,所以並不會顯示 “ok” .

root@localhost sbin]# if [ -f /root/test.txt ]; then echo ok; fi
ok
[root@localhost sbin]# if [ -r /root/test.txt ]; then echo ok; fi
ok
[root@localhost sbin]# if [ -w /root/test.txt ]; then echo ok; fi
ok
[root@localhost sbin]# if [ -x /root/test.txt ]; then echo ok; fi
[root@localhost sbin]# if [ -e /root/test1.txt ]; then echo ok; fi

在shell 指令碼中,除了用if來判斷邏輯外,還有一種常用的方式,那就是case了。具體格式為:

case  變數  in
value1)
          command
          ;;
value2)
          command
          ;;
value3)
          command
          ;;
*)
          command
          ;;
esac

上面的結構中,不限制value的個數,*則代表除了上面的value外的其他值。下面阿銘寫一個判斷輸入數值是奇數或者偶數的指令碼:

[root@localhost sbin]# cat case.sh
#! /bin/bash

read -p "Input a number: " n
a=$[$n%2]
case $a in

    1)
        echo "The number is odd."
        ;;
    0)
        echo "The number is even."
        ;;
    *)
        echo "It's not a number!"
        ;;
esac

$a 的值或為1或為0,執行結果為:

[root@localhost sbin]# sh case.sh
Input a number: 100
The number is even.
[root@localhost sbin]# sh case.sh
Input a number: 101
The number is odd.

case指令碼常用於編寫系統服務的啟動指令碼,例如/etc/init.d/iptables中就用到了,你不妨去檢視一下