Shell流程控制

2021-07-10 15:58:28 字數 1434 閱讀 8077

case迴圈

if condition

[ condition ... ]

then

statements-if-true-1

[ elif condition

[ condition ...]

then

statements-if-true-2

...]

[ else

statements-if-all-else-fails ]

fi

注:方括號中的內容非必須。

邏輯!、&&與||
if ! condition

then

: #冒號類似空操作

else

statements

fi

if cond1 && cond2

then

statements

fi

if cond1 || cond2

then

statements

fi

注:以上邏輯操作支援短路功能。

test 命令

一般使用test的退出狀態,其等效[ … ]。

case

$var

incond1)

....

;; #本選項結束

cond2)

....

;;*) .... #不需要;;

esac

for i in

list

do statements

done

>

注:list省略則表示遍歷整個命令列引數

while cond

do statements

done

until cond

do statements

done

**注:**until後面cond未成功退出,則迴圈繼續。

break與continue

break 與 continue控制迴圈的流程,類似於c語言,比起c語言,shell提供多層結束和多層跳出。

break num

continue num

參考:

1、shell指令碼學習指南。

Shell 流程控制

shell的流程控制不可為空,如果else分支沒有語句執行,就不要寫這個else。if 語句語法格式 if condition then command1 command2 commandn fi寫成一行 適用於終端命令提示符 if ps ef grep c ssh gt 1 then echo t...

Shell流程控制

shell 流程控制 if else if語法格式 if condition then command1 command2 fi 末尾的fi就是if倒過來拼寫 if else語法 if condition then command1 command2 else command1 fi if else...

Shell 流程控制

格式1 語法 if 條件 then commond fi eg bin bash a 5if a gt3 then echo 大於3 fi格式2 語法 if 條件1 then commond1 else commond2 fi eg bin bash a 5if a gt3 then echo 大於...