shell中的控制語句

2021-10-12 05:31:43 字數 1297 閱讀 9673

【參考】菜鳥教程、linux中編寫shell指令碼、linux 的基本操作(編寫shell 指令碼)

shell 語言中 0 代表 true,0 以外的值代表 false

###條件判斷##### 

if 條件判斷

then

命令 elif 條件判斷

then

命令 else

命令 fi ##或

if 條件判斷;then

命令 elif 條件判斷;then

命令 else

命令 fi

case條件語句相當於多分支的if/elif/ellse條件語句,但是它比這些條件語句看起來更規範更工整,常被應用於實現系統服務啟動指令碼等企業應用場景中。

case 變數 in 

one)

命令 ;; two)

命令 ;; *)

命令 esac

舉例:判斷資料型別

#!/bin/bash 

read -p "enter a number or a word: " i

case $i in

[1-9])

echo "this is a number"

;; [a-z])

echo "this is a word"

;; *)

echo "i don't know"

esac

for 條件 

do 命令

done

##或

for 條件;do

命令 done

##或

for var in item1 item2 ... itemn

do command1 command2 ... commandn

done

while迴圈語句

while 條件 

do 命令

done

舉例

#!/bin/bash 

for ((i=1;i<10;i++))

do echo $i

done

i=11

while ((i<20))

do echo $i

#((i++))

let i++

done

shell控制語句

1 if 語句 命令格式 bin bash score 75 if score lt 60 then echo ccccccc elif score ge 60 score lt 80 then echo bbbbbbb else score ge 80 echo aaaaaa fi注意 1 if語...

shell的控制流程語句

shell分為兩大類 bourne shell 包括 sh,ksh,bash bourne shell sh korn shell ksh bourne again shell bash posix shell sh c shell 包括 csh and tcsh c shell csh tenex...

shell 條件控制語句

if else命令 1 單分支if條件語句 if 條件判斷式 then 程式fi注意 1.if語句使用fi結尾,和一般語言使用大括號結尾不同。2.條件判斷式 就是使用test命令判斷,所以中括號和條件判斷式之間必須有空格 3.then後面跟符號條件之後執行的程式,可以放在之後,用 分割,也可以換行寫...