Shell教程day05

Shell 流程控制

和Java、PHP等語言不一樣,sh的流程控制不可爲空,如(以下爲PHP流程控制寫法):

<?php
if (isset($_GET["q"])) {
    search(q);
}
else {
    //do nothing
}

在sh/bash裏可不能這麼寫,如果else分支沒有語句執行,就不要寫這個else,就像這樣


if else

if

if 語句語法格式:

if condition
then
    command1 
    command2
    ...
    commandN 
fi

寫成一行(適用於終端命令提示符):

if `ps -ef | grep ssh`;  then echo hello; fi

末尾的fi就是if倒過來拼寫,後面還會遇到類似的。

if else

if else 語法格式:

if condition
then
    command1 
    command2
    ...
    commandN
else
    command
fi

if else-if else

if else-if else 語法格式:

if condition1
then
    command1
elif condition2
    command2
else
    commandN
fi

if else語句經常與test命令結合使用,如下所示:

num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
    echo 'The two numbers are equal!'
else
    echo 'The two numbers are not equal!'
fi

輸出結果:

The two numbers are equal!

for 循環

與其他編程語言類似,Shell支持for循環。

for循環一般格式爲:

for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done

寫成一行:

for var in item1 item2 ... itemN; do command1; command2… done;

當變量值在列表裏,for循環即執行一次所有命令,使用變量名獲取列表中的當前取值。命令可爲任何有效的shell命令和語句。in列表可以包含替換、字符串和文件名。

in列表是可選的,如果不用它,for循環使用命令行的位置參數。

例如,順序輸出當前列表中的數字:

for loop in 1 2 3 4 5
do
    echo "The value is: $loop"
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

輸出結果:

This is a string

while 語句

while循環用於不斷執行一系列命令,也用於從輸入文件中讀取數據;命令通常爲測試條件。其格式爲:

while condition
do
    command
done

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

以下是一個基本的while循環,測試條件是:如果COUNTER小於5,那麼條件返回真。COUNTER從0開始,每次循環處理時,COUNTER加1。運行上述腳本,返回數字1到5,然後終止。

COUNTER=0
while [ $COUNTER -lt 5 ]
do
    COUNTER='expr $COUNTER+1'
    echo $COUNTER
done

運行腳本,輸出:

1
2
3
4
5

while循環可用於讀取鍵盤信息。下面的例子中,輸入信息被設置爲變量FILM,按<Ctrl-D>結束循環。

echo 'type <CTRL-D> to terminate'
echo -n 'enter your most liked film: ''
while read FILM
do
    echo "Yeah! great film the $FILM"
done

運行腳本,輸出類似下面:

type <CTRL-D> to terminate
enter your most liked film: Sound of Music
Yeah! great film the Sound of Music

無限循環

無限循環語法格式:

while :
do
    command
done

或者

while true
do
    command
done

或者

for (( ; ; ))



until 循環

until循環執行一系列命令直至條件爲真時停止。

until循環與while循環在處理方式上剛好相反。

一般while循環優於until循環,但在某些時候—也只是極少數情況下,until循環更加有用。

until 語法格式:

until condition
do
    command
done

條件可爲任意測試條件,測試發生在循環末尾,因此循環至少執行一次—請注意這一點。


case

Shell case語句爲多選擇語句。可以用case語句匹配一個值與一個模式,如果匹配成功,執行相匹配的命令。case語句格式如下:

case 值 in
模式1)
    command1
    command2
    ...
    commandN
    ;;
模式2)
    command1
    command2
    ...
    commandN
    ;;
esac

case工作方式如上所示。取值後面必須爲單詞in,每一模式必須以右括號結束。取值可以爲變量或常數。匹配發現取值符合某一模式後,其間所有命令開始執行直至 ;;。

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

下面的腳本提示輸入1到4,與每一種模式進行匹配:

echo 'Input a number between 1 to 4'
echo 'Your number is:\c'
read aNum
case $aNum in
    1)  echo 'You select 1'
    ;;
    2)  echo 'You select 2'
    ;;
    3)  echo 'You select 3'
    ;;
    4)  echo 'You select 4'
    ;;
    *)  echo 'You do not select a number between 1 to 4'
    ;;
esac

輸入不同的內容,會有不同的結果,例如:

Input a number between 1 to 4
Your number is:3
You select 3

跳出循環

在循環過程中,有時候需要在未達到循環結束條件時強制跳出循環,Shell使用兩個命令來實現該功能:break和continue。

break命令

break命令允許跳出所有循環(終止執行後面的所有循環)。

下面的例子中,腳本進入死循環直至用戶輸入數字大於5。要跳出這個循環,返回到shell提示符下,需要使用break命令。

#!/bin/bash
while :
do
    echo -n "Input a number between 1 to 5: "
    read aNum
    case $aNum in
        1|2|3|4|5) echo "Your number is $aNum!"
        ;;
        *) echo "You do not select a number between 1 to 5, game is over!"
            break
        ;;
    esac
done

continue

continue命令與break命令類似,只有一點差別,它不會跳出所有循環,僅僅跳出當前循環。

對上面的例子進行修改:

#!/bin/bash
while :
do
    echo -n "Input a number between 1 to 5: "
    read aNum
    case $aNum in
        1|2|3|4|5) echo "Your number is $aNum!"
        ;;
        *) echo "You do not select a number between 1 to 5!"
            continue
            echo "Game is over!"
        ;;
    esac
done

運行代碼發現,當輸入大於5的數字時,該例中的循環不會結束,語句 echo "Game is over!" 永遠不會被執行。


esac

case的語法和C family語言差別很大,它需要一個esac(就是case反過來)作爲結束標記,每個case分支用右圓括號,用兩個分號表示break。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章