- if開(kāi)始判斷,反順序fi結(jié)束
- if后跟判斷條件,then后跟條件成立后,要執(zhí)行的代碼
- if不滿足的時(shí)候,則執(zhí)行else
- elif為當(dāng)上一個(gè)條件不滿足時(shí)才會(huì)執(zhí)行
- 判斷條件必須以 (()) 包括,不得缺少
- 判斷條件也可以使用 [] ,但[]和條件之間必須有空格,且條件的判斷不可再使用 < 這些符號(hào),需使用 -lt
| 符號(hào) |
代表符 |
含義 |
| = |
-eq |
等號(hào) |
| != |
-ne |
不等于 |
| > |
-gt |
大于 |
| >= |
-ge |
大于等于 |
| < |
-lt |
小于 |
| <= |
-le |
小于等于 |
- shell腳本中if還常判斷文件的屬性,使用[ -xxx filename ]
| 參數(shù) |
含義 |
| -e |
判斷文件或目錄是否存在 |
| -d |
判斷是否是目錄,且此目錄是否存在 |
| -f |
判斷是否是普通文件,且此文件是否存在 |
| -r |
判斷是否有讀權(quán)限 |
| -w |
判斷是否有寫(xiě)權(quán)限 |
| -x |
判斷是否有執(zhí)行權(quán)限 |
- shell語(yǔ)句中,一行若要寫(xiě)多條語(yǔ)句,則必須以 ; 分號(hào)分割.若換行,則無(wú)需 ; 分號(hào)結(jié)尾
#! /bin/bash
#Init var
score="Pls input your score :"
exam_not="Sorry ! You didn't pass the exam."
exam_yes="Congratulation ! You pass the exam."
# If
read -p "$score" a
if ((a<60));then
echo $exam_not
fi
# If & else
read -p "$score" b
if ((b<60));then
echo $exam_not
else
echo $exam_yes
fi
# If & else & elif
read -p "$score" c
if ((c<60))
then
echo $exam_not
elif ((c>=60 && c<=100));then
echo $exam_yes
else
echo "Sorry ! The information of you had inputted is wrong !"
fi
# []
d=10;if [ $d -gt 5 ];then echo "Pass !";fi
# File attr
if [ -r test01_if.sh ];then echo "Has !";fi
- case可以有無(wú)限個(gè)分支
- case語(yǔ)句必須以反數(shù)序單詞esac結(jié)束
- 選項(xiàng)*代表前面的選項(xiàng)不滿足時(shí)執(zhí)行的分支
- 每個(gè)分支必須以;;結(jié)束
#! /bin/bash
# Judge the number is even or odd !
read -p "Pls input a num : " a
b=$[$a%2]
case $b in
1)
echo "The num is even !"
;;
0)
echo "The num is odd !"
;;
*)
echo "It is error !"
;;
esac
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。