一、for命令
公司主營業(yè)務(wù):成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)公司推出通許免費(fèi)做網(wǎng)站回饋大家。
在shell編程中,有時(shí)我們需要重復(fù)執(zhí)行一直命令直至達(dá)到某個(gè)特定的條件,bash shell中,提供了for命令,允許你創(chuàng)建一個(gè)遍歷一系列值的循環(huán),每次迭代都通過一個(gè)該系列中的值執(zhí)行一組預(yù)定義的命令。
for的基本格式:
for var in list
do
commands
done
在list中,你提供了迭代中要用的一系列值。在每個(gè)迭代中,變量var包含列表中的當(dāng)前值,第一個(gè)迭代會適用列表中的第一個(gè)值,第二個(gè)迭代使用第二個(gè)值,以此類推,直至列表中的所有值都過一遍。
1.1讀取列表中的值
[root@sh shell]# cat for1.sh #!/bin/bash for test in aaa bbb ccc ddd do echo the next state is $test done [root@sh shell]# sh for1.sh the next state is aaa the next state is bbb the next state is ccc the next state is ddd
$test變量的值會在shell腳本的最后一次迭代中一直保持有效,除非你修改了它
[root@sh shell]# cat for1.sh #!/bin/bash for test in aaa bbb ccc ddd do echo the next state is $test done echo "the last state we visited was $test" test=fff echo "wait. now we're visiting $test" [root@sh shell]# sh for1.sh the next state is aaa the next state is bbb the next state is ccc the next state is ddd the last state we visited was ddd wait. now we're visiting fff
1.2讀取列表中的復(fù)雜值
在shell腳本中,優(yōu)勢你會遇到難處理的數(shù)。下面是個(gè)給shell腳本程序員帶來麻煩的經(jīng)典例子:
[root@sh shell]# cat for2.sh #!/bin/bash for test in I don't know if this'll work do echo "word:$test" done [root@sh shell]# sh for2.sh word:I word:dont know if thisll word:work
解決辦法:使用轉(zhuǎn)義符或者雙引號
[root@sh shell]# cat for2.sh #!/bin/bash for test in I don\'t know if "this'll" work do echo "word:$test" done [root@sh shell]# sh for2.sh word:I word:don't word:know word:if word:this'll word:work
記?。篺or循環(huán)假定每一個(gè)值都是用空格分割的,如果在單獨(dú)的數(shù)字值中有空格,那么你必須使用雙引號來將這些值圈起來。
1.3從變量讀取列表
[root@sh shell]# cat for3.sh ############################ #!/bin/bash list="aaa bbb ccc ddd eee" list=$list" Connecticut" for state in $list do echo "Have you ever visited $state" done [root@sh shell]# sh for3.sh Have you ever visited aaa Have you ever visited bbb Have you ever visited ccc Have you ever visited ddd Have you ever visited eee Have you ever visited Connecticut
1.4從命令讀取值
[root@sh shell]# cat for4.sh #!/bin/bash file="/root/shell/states" #如果是在當(dāng)前不用絕對路徑,file=“states”即可 for state in `cat $file` do echo "Visit beautiful $state" done [root@sh shell]# sh for4.sh Visit beautiful shanghai Visit beautiful beijing Visit beautiful hangzhou Visit beautiful nanjing Visit beautiful guangzhou [root@sh shell]# cat states shanghai beijing hangzhou nanjing guangzhou
1.5更改字段分隔符
空格;
制表符:
換行符
如果bash shell在數(shù)據(jù)中看到了這些字符中任意一個(gè),它就會假定你在列表中開始了一個(gè)新的數(shù)據(jù)段。
要解決這個(gè)問題,你可以在你shell腳本中臨時(shí)更改IFS環(huán)境變量的值來限制一下被bash shell當(dāng)作字段分隔符的字符。但這種方式有點(diǎn)奇怪,比如,如果你行IFS的值使其只能識別換行符,你必須這么做:
IFS=$'\n'
將這個(gè)語句加入到腳本中,告訴bash shell在數(shù)據(jù)值中忽略空格和制表符。
[root@sh shell]# cat for5.sh #!/bin/bash file="states" IFS=$'\n' for state in `cat $file` do echo "Visit beautiful $state" done [root@sh shell]# sh for5.sh Visit beautiful shanghai Visit beautiful beijing Visit beautiful hangzhou Visit beautiful nanjing Visit beautiful guang zhou Visit beautiful nan ning Visit beautiful jiang nan
在處理長腳本中,可能在一個(gè)地方需要修改IFS的值,然后忘掉它在腳本中其他地方還原默認(rèn)值。
例如:
IFS.OLD=$IFS
IFS=$'\n'
<use the new IFS value in code>
IFS=$IFS.OLD
其他的IFS值,如:在/etc/passwd中可能用到
IFS=:
也可以賦值多個(gè)IFS:
IFS=$'\n:;"'
1.6用通配符讀取目錄
[root@sh shell]# cat for6.sh #!/bin/bash for file in /home/* do if [ -d "$file" ];then echo "$file is a directory" elif [ -f "$file" ];then echo "$file is a file" fi done [root@sh shell]# sh for6.sh /home/apache-tomcat-8.0.28.tar.gz is a file /home/dir1 is a directory /home/dir2 is a directory /home/fie1 is a file /home/fie2 is a file /home/fie22 is a file
[root@sh shell]# cat for7.sh #!/bin/bash for file in /home/* /home/badtest do if [ -d "$file" ];then echo "$file is a directory" elif [ -f "$file" ];then echo "$file is a file" else echo "$file doesn't exist" fi done [root@sh shell]# sh for7.sh /home/apache-tomcat-8.0.28.tar.gz is a file /home/dir1 is a directory /home/dir2 is a directory /home/fie1 is a file /home/fie2 is a file /home/fie22 is a file /home/badtest doesn't exist
二、C語言風(fēng)格的for命令
2.1 C語言風(fēng)格的for命令
C語言的for命令有一個(gè)用來指明變量的特殊方法、一個(gè)必須保持成立才能繼續(xù)迭代的條件,以及另一個(gè)為每個(gè)迭代改變變量的方法。當(dāng)指定的條件不成立時(shí),for循環(huán)就會停止。條件等式通過標(biāo)準(zhǔn)的數(shù)字符號定義。
for (i=0; i<10; i++)
{
printf("The next number is %d\n",i):
}
第一部分將一個(gè)默認(rèn)值賦給該變量,中間的部分定義了循環(huán)重復(fù)的條件,當(dāng)定義的條件不成立時(shí),for循環(huán)就停止迭代,最后一部分定義了迭代的過程。
bash中C語言風(fēng)格的for循環(huán)的基本格式:
for (( variable assignment;condition;iteration process))
for (( a = 1; a < 10; a++ ))
[root@sh shell]# cat c1.sh #!/bin/bash for (( i=1; i < 10; i++ )) do echo "The next number is $i" done [root@sh shell]# sh c1.sh The next number is 1 The next number is 2 The next number is 3 The next number is 4 The next number is 5 The next number is 6 The next number is 7 The next number is 8 The next number is 9
2.2使用多個(gè)變量
C語言風(fēng)格的for命令也允許你為迭代使用多個(gè)變量。循環(huán)會單獨(dú)處理每個(gè)變量,允許你為每個(gè)變量定義不同的迭代過程。當(dāng)你有多個(gè)變量時(shí),你只能在for循環(huán)中定義一種條件:
[root@sh shell]# cat c2.sh #!/bin/bash for (( a=1, b=10; a <= 10; a++, b-- )) do echo "$a - $b" done [root@sh shell]# sh c2.sh 1 - 10 2 - 9 3 - 8 4 - 7 5 - 6 6 - 5 7 - 4 8 - 3 9 - 2 10 - 1
三、while命令
while命令在某種意義上是if-then語句和for循環(huán)的混雜體。while命令允許你定義一個(gè)要測試的命令,然后循環(huán)執(zhí)行一組命令,只要定義的測試命令返回的是退出狀態(tài)碼0,它就會在每個(gè)迭代的一開始測試test命令,在測試test命令返回非零退出狀態(tài)碼是,while命令會停止執(zhí)行那組命令。
3.1while的基本格式
while test command
do
other command
done
while命令指定的test命令的退出狀態(tài)碼必須隨著循環(huán)中運(yùn)行的命令改變,如果退出狀態(tài)碼從不改變,那么while循環(huán)將會一直不停地循環(huán)。
[root@sh shell]# cat w1.sh #!/bin/bash var1=10 while [ $var1 -gt 0 ] do echo $var1 var1=$[ $var1 - 1 ] done [root@sh shell]# sh w1.sh 10 9 8 7 6 5 4 3 2 1
在這些命令中,測試條件中用到的變量必須被修改,否則你就進(jìn)入了一個(gè)無限循環(huán)。
3.2使用多個(gè)測試命令
while命令允許你在while語句行定義多個(gè)測試命令,只有最后一個(gè)測試命令的退出狀態(tài)碼會被用來決定什么時(shí)候介紹循環(huán)。
[root@sh shell]# cat w2.sh #!/bin/bash var1=10 while echo $var1 [ $var1 -ge 0 ] do echo "This is inside the loop" var1=$[ $var1 - 1 ] done [root@sh shell]# sh w2.sh 10 This is inside the loop 9 This is inside the loop 8 This is inside the loop 7 This is inside the loop 6 This is inside the loop 5 This is inside the loop 4 This is inside the loop 3 This is inside the loop 2 This is inside the loop 1 This is inside the loop 0 This is inside the loop -1
在上面的例子中,while語句中定義了兩個(gè)測試命令:
while echo $var1
[ $var1 -ge 0 ]
在while語句中,在每次迭代中所有的測試命令都會被執(zhí)行,包括最后一個(gè)命令不成立的最后那次循環(huán)。注意每個(gè)測試命令都是在單獨(dú)的一行上。
四、until命令
until命令和while命令工作的方式完全相反,until命令要求你指定一個(gè)通常輸出非零退出狀態(tài)碼的測試命令,只有在測試命令的退出狀態(tài)碼非零,bash shell才會指定循環(huán)中列出的那些命令,一旦測試命令返回了退出狀態(tài)碼0,循環(huán)就結(jié)束了。
4.1 until命令的基本格式
until test commands
do
other commands
done
4.2until語句中測試多條命令
[root@www shell]# cat u1.sh #!/bin/bash var1=100 until [ $var1 -eq 0 ] do echo $var1 var1=$[ $var1 - 25 ] done [root@www shell]# sh u1.sh 100 75 50 25
[root@www shell]# cat u2.sh #!/bin/bash var1=100 until echo $var1 [ $var1 -eq 0 ] do echo Inside the loop:$var1 var1=$[ $var1 -25 ] done [root@www shell]# sh u2.sh 100 Inside the loop:100 75 Inside the loop:75 50 Inside the loop:50 25 Inside the loop:25 0
五、if-then-elif-then-fi
if command1
then
command set 1
elif command2
then
command set 2
elif command3
then
command set 3
fi
六、test命令
test命令中列出的條件成立時(shí),test命令會退出并返回退出狀態(tài)碼0,如果條件不成立,test命令就會退出并返回退出狀態(tài)碼1
test命令格式:
test condition(條件)
test和if語句使用的格式:
if test condition
then
commands
fi
跟下面類似
if [ condition ]
then
commands
fi
七、嵌套for循環(huán)
#!/bin/bash for (( a = 1; a <= 3; a++ )) do echo "Starting loop $a:" for (( b = 1; b <= 3; b++ )) do echo -e "\tInside loop: $b" done done # sh for1.sh Starting loop 1: Inside loop: 1 Inside loop: 2 Inside loop: 3 Starting loop 2: Inside loop: 1 Inside loop: 2 Inside loop: 3 Starting loop 3: Inside loop: 1 Inside loop: 2 Inside loop: 3
當(dāng)前標(biāo)題:shell編程中for/while/until循環(huán)命令
標(biāo)題網(wǎng)址:http://aaarwkj.com/article20/peicjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、做網(wǎng)站、微信小程序、網(wǎng)站排名、響應(yīng)式網(wǎng)站、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)