1. 程式人生 > >shell腳本從入門到復雜七(循環)

shell腳本從入門到復雜七(循環)

循環 if for while case

一、if循環

語法格式如下:


單分支的if語句:


if condition

then

command1

command2

...

commandN

fi


雙分支的if語句:


if condition

then

command1

command2

...

commandN

else

command

fi


多分支的if語句:


if condition1

then

command1

elif condition2

then

command2

else

commandN

fi


案例1:

if else單支循環可以直接在命令行組合運行

# if [ `awk -F: '/^root/ {print $3}' /etc/passwd` -eq 0 ];then ehco "true";fi


案例2:

雙支循環判斷當前shell環境

# vi chkshell.sh

#!/bin/bash

if [ $SHELL = "/bin/bash" ];then

echo "your shell is bash"

else

echo "your shell is not bash but $SHELL"

fi


案例3:

多支循環比較兩個數的大小

# vi if1.sh

#!/bin/bash

a=10

b=20

if [ $a == $b ];then

echo "a is equal than b"

elif [ $a -gt $b ];then

echo "a is greater than b"

elif [ $a -lt $b ];then

echo "a is less than b"

else

echo "None of the condition met"

fi


二、for循環

格式:

for var in item1 item2 ... itemN

do

command1

command2

...

commandN

done


for ((expr1;expr2;expr3));do

command1

command2

...

commandN

done


案例1:

計算1到100的和

# vi for1.sh

#!/bin/bash

sum=0

for in in {1..100};do

let sum=$sum+$i

done

echo $sum


案例2:

計算1到100的和

# vi for2.sh

#!/bin/bash

sum=0

for((i=1;i<=100;i++));do

sum=$(($sum+$i))

done

echo $sum


三、while 語句

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

while condition

do

command

done


案例1:

計算1到100的和

# vi wihle1.sh

#!/bin/bash

sum=0

i=1

while [ $i -le 100 ];do

((sum+=1))

((i++))

done

echo $sum


四、until語句

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

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

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

until 語法格式:

until condition

do

command

done


condition 一般為條件表達式,如果返回值為 false,則繼續執行循環體內的語句,否則跳出循環。


案例1:

計算1到100的和

# vi until1.sh

#!/bin/bash

sum=0

i=1

until [ $i -gt 100 ];do

((sum+=i))

((i++))

done

echo $sum


五、case語句

case循環:

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

case 值 in

模式1)

command1

command2

...

commandN

;;

模式2)

command1

command2

...

commandN

;;

esac


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

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


案例1:

類似./nginx start這樣的操作服務的腳本簡單版

# vi case1.sh

#!/bin/bash

case $1 in

start)

echo "service is running"

;;

stop)

echo "service is stop"

;;

reload)

echo "service is relaod"

;;

*)

echo "usage:[start|stop|reload]"

;;

esac


# sh case1.sh start

service is running


案例2:

輸入一個字符,判斷該字符是字母、數字或是其他

# vi case2.sh

#!/bin/bash

read -p "press one key,then press return: " KEY

case $KEY in

[a-z]|[A-Z])

echo "It's a letter"

;;

[0-9])

echo "It's a digit"

;;

*)

echo "It's function keys,Spacebar other keys"

esac


六、跳出循環

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


6.1、break語句

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

案例1:

腳本進入死循環,直到輸入的字符在1到5之外

# vi break1.sh

#!/bin/bash

while :

do

echo -n "please enter 1 to 5: "

read num

case $num in

1|2|3|4|5)

echo "your input number is $num"

;;

*)

echo "your number is error,over"

break

;;

esac

done


案例2:

數字到5跳出循環

# vi break2.sh

#!/bin/bash

i=1

while [ $i -lt 10 ];do

echo $i

if [ $i -eq 5 ]

then

break

fi

((i+=1))

done


執行結果就是輸出

1

2

3

4

5


6.2、continue語句

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

# vi continue1.sh

#!/bin/bash

while :

do

echo -n "please enter 1 to 5: "

read num

case $num in

1|2|3|4|5)

echo "your input number is $num"

;;

*)

echo "your number is error"

continue

echo "over"

;;

esac

done


執行結果是,當輸入1到5之外的字符時不會跳出循環,語句 echo "over" 不會執行。


shell腳本從入門到復雜七(循環)