[shell]shell中if語句的使用
阿新 • • 發佈:2017-06-25
don 條件 如何 ood 字符串 pre 評分 string 另一個
轉自:http://lovelace.blog.51cto.com/1028430/1211353
if 判斷條件;then statement1 statement2 ....... fi
if 判斷條件;then statement1 statement2 ..... else statement3 statement4 fi
[ ! "$name" ] [ -z "$name" ]
if [ $x -gt 90 -o $x -lt 100 ] case $x in 100) 9[0-9])
if [ "X$name" != "x" ]
#/bin/bash #Verson:0.1 #Auther:lovelace #Pragram:This pragram is calculation your grade #import an argument read -p "Please input your grade:" x declare -i x #jugemet $x value is none or not if [ "$x" == "" ];then echo "You don‘t input your grade...." exit 5 fi #jugement the gread level if [[ "$x" -ge "90" && "$x" -le "100" ]];then echo "Congratulation,Your grade is A." elif [[ "$x" -ge "80" && "$x" -le "89" ]];then echo "Good,Your grade is B." elif [[ "$x" -ge "70" && "$x" -le "79" ]];then echo "Ok.Your grade is C." elif [[ "$x" -ge "60" && "$x" -le "69" ]];then echo "Yeah,Your grade is D." elif [[ "$x" -lt "60" ]];then echo "Right,Your grade is F." else echo "Unknow argument...." fi
執行結果:
[[email protected] if]# ./grade.sh Please input your grade: You don‘t input your grade.... [[email protected] if]# ./grade.sh Please input your grade:53 Right,Your grade is F. [[email protected] if]# ./grade.sh Please input your grade:88 Good,Your grade is B.
總結:條件判斷在shell語句中經常用到,需要熟練掌握,在此基礎上才能練就一手很好的腳本編寫能力。祝各位每天都能獲得很大的進步.....
[shell]shell中if語句的使用