1. 程式人生 > >shell腳本中的邏輯判斷

shell腳本中的邏輯判斷

意思 case code shel 大於等於 不用 com 格式 ocr

shell腳本中的邏輯判斷

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

1)不帶else

if  判斷語句; then
    command
fi

例如:

[[email protected] 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 ]。執行結果為:

[[email protected] sbin]# sh if1.sh
Please input your score: 90
[[email protected] sbin]# sh if1.sh
Please input your score: 33
You didn‘t pass the exam.

2)帶有else

if  判斷語句  ; then
    command
else
    command
fi

例如:

[[email protected] 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

執行結果:

[[email protected] sbin]# sh if2.sh
Please input your score: 80
Good! You passed the exam.
[[email protected] 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

例如:

[[email protected] 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

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

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

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

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

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

[[email protected] sbin]# a=10; if [ $a -lt 1 ] || [ $a -gt 5 ]; then echo ok; fi
ok
[[email protected] 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 [ -e filename ] ; then

例子:

[[email protected] sbin]# if [ -d /home/ ]; then echo ok; fi
ok
[[email protected] sbin]# if [ -f /home/ ]; then echo ok; fi

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

[[email protected] sbin]# if [ -f /root/test.txt ]; then echo ok; fi
ok
[[email protected] sbin]# if [ -r /root/test.txt ]; then echo ok; fi
ok
[[email protected] sbin]# if [ -w /root/test.txt ]; then echo ok; fi
ok
[[email protected] sbin]# if [ -x /root/test.txt ]; then echo ok; fi
[[email protected] 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外的其他值。下面阿銘寫一個判斷輸入數值是奇數或者偶數的腳本:

[[email protected] 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,執行結果為:

[[email protected] sbin]# sh case.sh
Input a number: 100
The number is even.
[[email protected] sbin]# sh case.sh
Input a number: 101
The number is odd.

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

shell腳本中的邏輯判斷