Shell腳本的條件控制和循環(huán)語(yǔ)句
條件判斷:if語(yǔ)句
語(yǔ)法格式:
if [ expression ] then Statement(s) to be executed if expression is true fi
注意:expression 和方括號(hào)([ ])之間必須有空格,否則會(huì)有語(yǔ)法錯(cuò)誤。
if 語(yǔ)句通過關(guān)系運(yùn)算符判斷表達(dá)式的真假來(lái)決定執(zhí)行哪個(gè)分支。Shell 有三種 if ... else 語(yǔ)句:
if ... fi 語(yǔ)句 if ... else ... fi 語(yǔ)句 if ... elif ... else ... fi 語(yǔ)句
示例:
#!/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 語(yǔ)句也可以寫成一行,以命令的方式來(lái)運(yùn)行:
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 語(yǔ)句也經(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語(yǔ)句
case ... esac 與其他語(yǔ)言中的 switch ... case 語(yǔ)句類似,是一種多分枝選擇結(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語(yǔ)言是:
#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; }
對(duì)比看就很容易理解了。很相似,只是格式不一樣。
需要注意的是:
取值后面必須為關(guān)鍵字 in,每一模式必須以右括號(hào)結(jié)束。取值可以為變量或常數(shù)。匹配發(fā)現(xiàn)取值符合某一模式后,其間所有命令開始執(zhí)行直至 ;;。;; 與其他語(yǔ)言中的 break 類似,意思是跳到整個(gè) case 語(yǔ)句的最后。
取值將檢測(cè)匹配的每一個(gè)模式。一旦模式匹配,則執(zhí)行完匹配模式相應(yīng)命令后不再繼續(xù)其他模式。如果無(wú)一匹配模式,使用星號(hào) * 捕獲該值,再執(zhí)行后面的命令。
再舉一個(gè)例子:
#!/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
運(yùn)行結(jié)果:
$./test.sh test.sh: usage: [ -f filename ] | [ -d directory ] ./test.sh -f index.html File name is index.html
這里用到了特殊變量${1},指的是獲取命令行的第一個(gè)參數(shù)。
for循環(huán)
shell的for循環(huán)與c、php等語(yǔ)言不同,同Python很類似。下面是語(yǔ)法格式:
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
運(yùn)行結(jié)果:
This is a string
遍歷目錄下的文件:
#!/bin/bash for FILE in * do echo $FILE done
上面的代碼將遍歷當(dāng)前目錄下所有的文件。在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)頂部,從頭開始直至測(cè)試條件為假。
示例:
#!/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ù)運(yùn)算,所以使用expr命令進(jìn)行自增。
until循環(huán)
until 循環(huán)執(zhí)行一系列命令直至條件為 true 時(shí)停止。until 循環(huán)與 while 循環(huán)在處理方式上剛好相反。一般while循環(huán)優(yōu)于until循環(huán),但在某些時(shí)候,也只是極少數(shù)情況下,until 循環(huán)更加有用。
將上面while循環(huán)的例子改改,就能達(dá)到一樣的效果:
#!/bin/bash c=0; until [ $c -eq 3 ] do echo "Value c is $c" c=`expr $c + 1` done
首先do里面的語(yǔ)句塊一直在運(yùn)行,直到滿足了until的條件就停止。
輸出:
Value c is 0
Value c is 1
Value c is 2
跳出循環(huán)
在循環(huán)過程中,有時(shí)候需要在未達(dá)到循環(huán)結(jié)束條件時(shí)強(qiáng)制跳出循環(huán),像大多數(shù)編程語(yǔ)言一樣,Shell也使用 break 和 continue 來(lái)跳出循環(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
運(yùn)行結(jié)果:
1
2
在嵌套循環(huán)中,break 命令后面還可以跟一個(gè)整數(shù),表示跳出第幾層循環(huán)。例如:
break n
表示跳出第 n 層循環(huán)。
continue
continue命令與break命令類似,只有一點(diǎn)差別,它不會(huì)跳出所有循環(huán),僅僅跳出當(dāng)前循環(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
運(yùn)行結(jié)果:
1
2
4
5
以上內(nèi)容是小編給大家介紹的Shell腳本的條件控制和循環(huán)語(yǔ)句的相關(guān)知識(shí),希望對(duì)大家有所幫助!
- Shell循環(huán)語(yǔ)句及中斷語(yǔ)句的使用
- shell編程中for循環(huán)語(yǔ)句的實(shí)現(xiàn)過程及案例
- shell腳本實(shí)戰(zhàn)-while循環(huán)語(yǔ)句
- shell腳本編程之循環(huán)語(yǔ)句
- shell腳本編程之循環(huán)語(yǔ)句學(xué)習(xí)筆記
- shell中的循環(huán)語(yǔ)句、判斷語(yǔ)句實(shí)例
- Shell腳本while、until循環(huán)語(yǔ)句簡(jiǎn)明教程
- Shell腳本for循環(huán)語(yǔ)句簡(jiǎn)明教程
- Shell中的循環(huán)語(yǔ)句for、while、until實(shí)例講解
- shell基礎(chǔ)學(xué)習(xí)中的字符串操作、for循環(huán)語(yǔ)句示例
- Shell循環(huán)語(yǔ)句的使用(for循環(huán)、while循環(huán)、until循環(huán))
相關(guān)文章
Bash的For循環(huán)(根據(jù)每次遞增的數(shù))
需要執(zhí)行一個(gè)命令,每次消除500個(gè)id,通常寫的是遞增1的,記一筆,怕忘了2013-08-08linux?shell?解析命令行參數(shù)及while?getopts用法小結(jié)
這篇文章主要介紹了linux?shell?解析命令行參數(shù)及while?getopts用法,getpots是Shell命令行參數(shù)解析工具,旨在從Shell?Script的命令行當(dāng)中解析參數(shù),本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2022-05-05shell腳本實(shí)現(xiàn)快速生成xml格式sitemap實(shí)例分享
這篇文章主要介紹了shell腳本實(shí)現(xiàn)快速生成xml格式sitemap實(shí)例分享,只是本文的腳本首先需要一個(gè)創(chuàng)建好的URL集合文件,也就是數(shù)據(jù)源才可以生成,需要的朋友可以參考下2014-12-12shell監(jiān)控腳本實(shí)例—監(jiān)控mysql主從復(fù)制
分享一例shell腳本,用于監(jiān)測(cè)mysql數(shù)據(jù)庫(kù)的主從復(fù)制,有需要的朋友不妨參考學(xué)習(xí)下2013-11-11