shell指令碼迴圈語句

2021-09-11 06:46:27 字數 3484 閱讀 5701

條件判斷:if語句

語法格式:

if [ expression ]

then

statement(s) to be executed if expression is true

fi

注意:expression 和方括號([ ])之間必須有空格,否則會有語法錯誤。

if 語句通過關係運算子判斷表示式的真假來決定執行哪個分支。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"

fiif ... 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 語句也經常與 test 命令結合使用,作用與上面一樣:

#!/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 語句類似,是一種多分枝選擇結構。

示例:對比看就很容易理解了。很相似,只是格式不一樣。

需要注意的是:

取值後面必須為關鍵字 in,每一模式必須以右括號結束。取值可以為變數或常數。匹配發現取值符合某一模式後,其間所有命令開始執行直至 ;;。;; 與其他語言中的 break 類似,意思是跳到整個 case 語句的最後。

取值將檢測匹配的每乙個模式。一旦模式匹配,則執行完匹配模式相應命令後不再繼續其他模式。如果無一匹配模式,使用星號 * 捕獲該值,再執行後面的命令。

需要注意的是:

取值後面必須為關鍵字 in,每一模式必須以右括號結束。取值可以為變數或常數。匹配發現取值符合某一模式後,其間所有命令開始執行直至 ;;。;; 與其他語言中的 break 類似,意思是跳到整個 case 語句的最後。

取值將檢測匹配的每乙個模式。一旦模式匹配,則執行完匹配模式相應命令後不再繼續其他模式。如果無一匹配模式,使用星號 * 捕獲該值,再執行後面的命令。

再舉乙個例子:

#!/bin/bash

option="$"

case $ in

"-f") file="$"

echo "file name is $file"

;;"-d") dir="$"

echo "dir name is $dir"

;;*)

echo "`basename $`:usage: [-f file] | [-d directory]"

exit 1 # command to come out of the program with status 1

;;esac

執行結果:

$./test.sh

test.sh: usage: [ -f filename ] | [ -d directory ]

./test.sh -f index.html

file name is index.html

這裡用到了特殊變數$,指的是獲取命令列的第乙個引數。

for迴圈

shell的for迴圈與c、php等語言不同,同python很類似。下面是語法格式:

for 變數 in 列表

docommand1

command2

...commandn

done

示例:

#!/bin/bash/

for value in 1 2 3 4 5

doecho "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'

doecho $str

done

執行結果:

this is a string

遍歷目錄下的檔案:

#!/bin/bash

for file in *

doecho $file

done

上面的**將遍歷當前目錄下所有的檔案。在linux下,可以改為其他目錄試試。

遍歷檔案內容:

city.txt

beijing

tianjin

shanghai

#!/bin/bash

citys=`cat city.txt`

for city in $citys

echo $city

done

輸出:

beijing

tianjin

shanghai

while迴圈

只要while後面的條件滿足,就一直執行do裡面的**塊。

其格式為:

while command

dostatement(s) to be executed if command is true

done

命令執行完畢,控制返回迴圈頂部,從頭開始直至測試條件為假。

示例:

#!/bin/bash

c=0;

while [ $c -lt 3 ]

doecho "value c is $c"

c=`expr $c + 1`

done

輸出:

value c is 0

value c is 1

value c is 2

這裡由於shell本身不支援算數運算,所以使用expr命令進行自增。

Shell指令碼中的迴圈語句

這裡包括for while until迴圈,以及變數自增的語法例項。一 for迴圈語句 例項1.1 最基本的for迴圈 傳統的形式,for var in 1 bin bash 2for x in one two three four3do 4echo number x 5 done 執行結果 1 r...

shell指令碼實戰 while迴圈語句

上文我們討論了for迴圈的使用,在有限迴圈裡,我們使用for迴圈是很方便的一件事情,今天我們來 下while迴圈 while迴圈語句的語法分析 語法格式一 while 條件 do 操作 done 語法格式二 while read line do 操作 done file 通過read命令每次讀取一行...

shell指令碼程式設計之迴圈語句

在執行指令碼時重複執行一系列的命令是很常見的,這時我們就需要使用迴圈語句來達到這個目的。一 for命令 格式 for 變數 in 列表 do 迴圈體done for命令會遍歷列表中的每乙個值,並且在遍歷完成後退出迴圈。列表形式有以下幾種 1 在命令中定義的一系列的值 2 變數 3 命令 4 目錄 5...