20.5 shell腳本中的邏輯判斷 文件目錄屬性判斷 if特殊用法

shell腳本中的邏輯判斷

不帶else

[root@aminglinux-128 ~]# vim if1.sh
[root@aminglinux-128 ~]# sh if1.sh
Please input your score: 90
You didn't pass the exam.
[root@aminglinux-128 ~]# vim if1.sh
[root@aminglinux-128 ~]# sh if1.sh
Please input your score: 33
You didn't pass the exam.

上例中編輯的腳本

#! /bin/bash
read -p "Please input your score: "33
if ((a<60)); then
    echo "You didn't pass the exam."
fi
#! /bin/bash
read -p "Please input your score: "90
if ((a<60)); then
    echo "You didn't pass the exam."
fi

帶有else

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

腳本執行結果

[root@aminglinux-128 ~]# sh if2.sh
Please input your score: 25
You didn't pass the exam.
[root@aminglinux-128 ~]# vim if2.sh
[root@aminglinux-128 ~]# sh if2.sh
Please input your score: 80

帶有elif

vim 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 score is very high!"
fi

腳本輸出結果

[root@aminglinux-128 ~]# vim if3.sh
[root@aminglinux-128 ~]# sh if3.sh
please input your score: 90
You didn't pass the exam.
[root@aminglinux-128 ~]# vim if3.sh
[root@aminglinux-128 ~]# sh if3.sh
please input your score: 60
You didn't pass the exam.

和文檔相關的判斷

shell腳本中的if用於判斷文檔的屬性,比如判斷是普通文件還是目錄,判斷文件是否有讀、寫、執行權限。

-e:判斷文件或目錄是否存在

-d:判斷是不是目錄以及是否存在

-f:判斷是不是普通文件以及是否存在

-r:判斷是否有讀權限

-w:判斷是否有寫權限。

-x:判斷是否可執行。

使用if判斷時的具體格式:

if [ -e filename ] ; then

command

fi

示例 

[root@aminglinux-128 ~]# if [ -d /home/ ];then echo ok; fi
ok
[root@aminglinux-128 ~]# if [ -f /home/ ];then echo ok; fi

因爲/home/是目錄而非文件,所以並不會顯示ok。

[root@aminglinux-128 ~]# if [ -f /home/ ];then echo ok; fi
[root@aminglinux-128 ~]# if [ -f /root/text.txt ];then echo ok; fi
[root@aminglinux-128 ~]# if [ -r /root/text.txt ];then echo ok; fi
[root@aminglinux-128 ~]# if [ -w /root/text.txt ];then echo ok; fi
[root@aminglinux-128 ~]# if [ -x /root/text.txt ];then echo ok; fi
[root@aminglinux-128 ~]# if [ -e /root/text.txt ];then echo ok; fi

case邏輯判斷

格式爲

case   變量  in

value1)

                 command

                 ;;

value2)

                 command

                 ;;

value3)

                 command

                 ;;

*)                   

                 command

                 ;;

esac

上面的結構中,不限制value的個數,*代表其他值。下面寫一個判斷輸入數值是奇數還是偶數的腳本。

vim case.sh
#! /bin/bash
read -p "Input anumber: " 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@aminglinux-128 ~]# sh case.sh
Input anumber: 100
The number is even.
[root@aminglinux-128 ~]# sh case.sh
Input anumber: 101
The number is odd.

case腳本常用於編寫系統服務的啓動腳本。

                 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章