條件判斷:if語句
語法格式:
if [ expression ] then Statement(s) to be executed if expression is true fi
注意:expression 和方括號([ ])之間必須有空格,否則會有語法錯誤。
if 語句通過關系運算符判斷表達式的真假來決定執(zhí)行哪個分支。Shell 有三種 if ... else 語句:
if ... fi 語句 if ... else ... fi 語句 if ... elif ... else ... fi 語句
示例:
#!/bin/bash/ a=10 b=20 if [ $a == $b ] then echo "a is equal to b" elif [ $a -gt $b ] then echo "a is greater to b" else echo "a is less to b" fi
if ... else 語句也可以寫成一行,以命令的方式來運行:
a=10;b=20;if [ $a == $b ];then echo "a is equal to b";else echo "a is not equal to b";fi;
if ... else 語句也經(jīng)常與 test 命令結(jié)合使用,作用與上面一樣:
#!/bin/bash/ a=10 b=20 if test $a == $b then echo "a is equal to b" else echo "a is not equal to b" fi
分支控制:case語句
case ... esac 與其他語言中的 switch ... case 語句類似,是一種多分枝選擇結(jié)構(gòu)。
示例:
#!/bin/bash/ grade="B" case $grade in "A") echo "Very Good!";; "B") echo "Good!";; "C") echo "Come On!";; *) echo "You Must Try!" echo "Sorry!";; esac
轉(zhuǎn)換成C語言是:
#include stdio.h> int main(){ char grade = 'B'; switch(grade){ case 'A': printf("Very Good!");break; case 'B': printf("Very Good!");break; case 'C': printf("Very Good!");break; default: printf("You Must Try!"); printf("Sorry!"); break; } return 0; }
對比看就很容易理解了。很相似,只是格式不一樣。
需要注意的是:
取值后面必須為關鍵字 in,每一模式必須以右括號結(jié)束。取值可以為變量或常數(shù)。匹配發(fā)現(xiàn)取值符合某一模式后,其間所有命令開始執(zhí)行直至 ;;。;; 與其他語言中的 break 類似,意思是跳到整個 case 語句的最后。
取值將檢測匹配的每一個模式。一旦模式匹配,則執(zhí)行完匹配模式相應命令后不再繼續(xù)其他模式。如果無一匹配模式,使用星號 * 捕獲該值,再執(zhí)行后面的命令。
再舉一個例子:
#!/bin/bash option="${1}" case ${option} in "-f") FILE="${2}" echo "File name is $FILE" ;; "-d") DIR="${2}" echo "Dir name is $DIR" ;; *) echo "`basename ${0}`:usage: [-f file] | [-d directory]" exit 1 # Command to come out of the program with status 1 ;; esac
運行結(jié)果:
$./test.sh test.sh: usage: [ -f filename ] | [ -d directory ] ./test.sh -f index.html File name is index.html
這里用到了特殊變量${1},指的是獲取命令行的第一個參數(shù)。
for循環(huán)
shell的for循環(huán)與c、php等語言不同,同Python很類似。下面是語法格式:
for 變量 in 列表
do command1 command2 ... commandN done
示例:
#!/bin/bash/ for value in 1 2 3 4 5 do echo "The value is $value" done
輸出:
The value is 1 The value is 2 The value is 3 The value is 4 The value is 5
順序輸出字符串中的字符:
for str in 'This is a string' do echo $str done
運行結(jié)果:
This is a string
遍歷目錄下的文件:
#!/bin/bash for FILE in * do echo $FILE done
上面的代碼將遍歷當前目錄下所有的文件。在Linux下,可以改為其他目錄試試。
遍歷文件內(nèi)容:
city.txt
beijing tianjin shanghai #!/bin/bash citys=`cat city.txt` for city in $citys echo $city done
輸出:
beijing
tianjin
shanghai
while循環(huán)
只要while后面的條件滿足,就一直執(zhí)行do里面的代碼塊。
其格式為:
while command
do
Statement(s) to be executed if command is true
done
命令執(zhí)行完畢,控制返回循環(huán)頂部,從頭開始直至測試條件為假。
示例:
#!/bin/bash c=0; while [ $c -lt 3 ] do echo "Value c is $c" c=`expr $c + 1` done
輸出:
Value c is 0
Value c is 1
Value c is 2
這里由于shell本身不支持算數(shù)運算,所以使用expr命令進行自增。
until循環(huán)
until 循環(huán)執(zhí)行一系列命令直至條件為 true 時停止。until 循環(huán)與 while 循環(huán)在處理方式上剛好相反。一般while循環(huán)優(yōu)于until循環(huán),但在某些時候,也只是極少數(shù)情況下,until 循環(huán)更加有用。
將上面while循環(huán)的例子改改,就能達到一樣的效果:
#!/bin/bash c=0; until [ $c -eq 3 ] do echo "Value c is $c" c=`expr $c + 1` done
首先do里面的語句塊一直在運行,直到滿足了until的條件就停止。
輸出:
Value c is 0
Value c is 1
Value c is 2
跳出循環(huán)
在循環(huán)過程中,有時候需要在未達到循環(huán)結(jié)束條件時強制跳出循環(huán),像大多數(shù)編程語言一樣,Shell也使用 break 和 continue 來跳出循環(huán)。
break
break命令允許跳出所有循環(huán)(終止執(zhí)行后面的所有循環(huán))。
#!/bin/bash i=0 while [ $i -lt 5 ] do i=`expr $i + 1` if [ $i == 3 ] then break fi echo -e $i done
運行結(jié)果:
1
2
在嵌套循環(huán)中,break 命令后面還可以跟一個整數(shù),表示跳出第幾層循環(huán)。例如:
break n
表示跳出第 n 層循環(huán)。
continue
continue命令與break命令類似,只有一點差別,它不會跳出所有循環(huán),僅僅跳出當前循環(huán)。
#!/bin/bash i=0 while [ $i -lt 5 ] do i=`expr $i + 1` if [ $i == 3 ] then continue fi echo -e $i done
運行結(jié)果:
1
2
4
5
以上內(nèi)容是小編給大家介紹的Shell腳本的條件控制和循環(huán)語句的相關知識,希望對大家有所幫助!
標簽:自貢 鎮(zhèn)江 百色 滁州 丹東 六盤水 武漢 優(yōu)質(zhì)小號
巨人網(wǎng)絡通訊聲明:本文標題《Shell腳本的條件控制和循環(huán)語句》,本文關鍵詞 Shell,腳本,的,條件,控制,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關。