標籤:
①if-elif-elif-else-fi
if-fi
` if(($a > 30));then echo $a;fi ` or ` if [ $a -gt 30 ];then echo $a;fi `
if-else-fi
` if(($a = 30));then echo $a;else echo 30;fi ` or ` if [ $a -eq 30 ];then echo $a;else echo 30;fi `
if-elif-elif-else-fi
` if(($a > 30)) && (($a = 30));then echo $a;elif(($a > 20)) || (($a = 20));then echo $[$a+1];else echo ‘Sorry‘;fi `
備忘:如果用方括弧[]包裹條件,則if 與 [之間、運算元與[ 、]之間必須有空格
> *** -gt = *** -eq >= *** -ge <= *** -le < *** -lt != *** -ne
if 判斷文件屬性
` if [ -defrwx filename ] ` -d 存在&&目錄 -e 存在 -f 存在&&普通檔案 -r 可讀 -w 可寫 -x 可執行
②case $a in value1) command;; value2) command;; value3) command;; *) command;; esac
` read -p ‘Please input a number: ‘ a;
case $[ $a%2 ] in
0)
echo "The number is even!";;
1)
echo "The number is odd!";;
*)
echo "This is impossible!";;
esac `
③for迴圈
for 變數名 in 迴圈條件;do command[;] done
eg:
a=`seq 1 10`;
for i in $a ;do echo $i ;done or for i in `seq 1 10`;do echo $i;done or for i in 1 2 3 4 5;do echo $i;done
備忘:`seq a b`表示一個從a到b的序列,例如`seq 1 10`產生一個從1到10的序列
④while迴圈
while 條件;do command;done
eg:
a=10;
while [ $a -ge 1 ];do
echo $a;
a=$[$-1];
done
SHELL指令碼的文法知識——流程式控制制