天天看點

shell程式設計之循環

一、for循環

for循環是Shelll中最常見的循環結構,根據書寫習慣又分為清單for循環、不帶清單的for循環以及類C的for循環。for循環是一種運作前的測試語句,也就是在運作任何循環體之前先要判斷循環條件是否成立,隻有在條件成立的情況下才會運作循環體,否則将會退出循環。每完成一次循環後,在進行下一次循環之前都會再次進行測試。

1.帶清單的for循環

[root@Cfhost-170820-UCNK ~]# cat fruit01.sh

#!/bin/bash

fruits="apple orange banana pear"

for FRUIT in $fruits

do

echo "$FRUIT is John's favorite"

done

echo "No more fruits"

[root@Cfhost-170820-UCNK ~]# sh fruit01.sh

apple is John's favorite

orange is John's favorite

banana is John's favorite

pear is John's favorite

No more fruits

2.不帶清單的for循環

for VARIABLE in $@

echo -n $VARIABLE

[root@Cfhost-170820-UCNK ~]# bash for_list07.sh 1 2 3

1 2 3

 3.類C的for循環

計算1到100的和和以及1到100的奇數和

[root@Cfhost-170820-UCNK ~]# cat c_for03.sh

sum01=0

sum02=0

for (( i=1,j=1;i<=100;i++,j+=2))

let "sum01+=i"

if [ $j -lt 100 ];then

let "sum02+=j"

fi

echo "sum01=$sum01"

echo "sum02=$sum02"

[root@Cfhost-170820-UCNK ~]# sh c_for03.sh

sum01=5050

sum02=2500

4.for的無限循環

無限循環又叫“死循環",要注意的是:和代碼設計功能無關的無限循環,或者說是開發者意料之外的無限循環都屬于軟體bug,這類bug容易造成系統資源耗盡,造成嚴重的系統故障,是以要非常小心,避免出現這種問題。開發者在用循環語句的時候要尤其注意循環結束條件,有條件的要進行測試。

死循環:

for ((;1;))

  echo "infinite loop"

5.while循環

用while循環做猜字遊戲

[root@Cfhost-170820-UCNK ~]# cat while03.sh

PRE_SET_NUM=8

echo "Input a number between 1 and 10"

while read GUESS

if [[ $GUESS -eq $PRE_SET_NUM ]];then

echo "you get the right number"

exit

else

echo "Wrong,try again"

用while按行讀取檔案

[root@Cfhost-170820-UCNK ~]# cat while04.sh

while read LINE

Name=`echo $LINE | awk '{print $1}'`

AGE=`echo $LINE | awk '{print $2}'`

Sex=`echo $LINE | awk '{print $3}'`

echo "My name is $Name, I'm $AGE years old, I'm a $Sex"

done < student_info.txt

[root@Cfhost-170820-UCNK ~]# cat student_info.txt

John 30 Boy

Sue 28 Girl

Wang 25 Boy

Xu 23 Girl

##上述這種方式,實際應用場景是可以把參數定義在另外一個檔案夾,這時如果我需要改動代碼傳遞參數,隻需改配置檔案即可

6.until循環

until循環也是運作前測試,但是until采用的是測試假值的方式,當測試結果為假時才繼續執行循環體,知道測試為真時,才停止循環

下面使用until計算1到100的和以及1到100的奇數和

[root@Cfhost-170820-UCNK ~]# cat until01.sh

i=1

until [[ $i -gt 100 ]]

let "j=i%2"

if [[ $j -ne 0 ]];then

let "sum02+=i"

let "i+=1"

done

echo $sum01

echo $sum02

[root@Cfhost-170820-UCNK ~]# sh until01.sh

5050

2500

7.select循環

[root@Cfhost-170820-UCNK ~]# cat select01.sh

echo "which car do you prefer?"

select CAR in Benz Audi VolksWagen

break

echo "You chose $CAR"

[root@Cfhost-170820-UCNK ~]# sh select01.sh

which car do you prefer?

1) Benz

2) Audi

3) VolksWagen

#? 2

You chose Audi

8.循環控制

#break

九九乘法表

[root@Cfhost-170820-UCNK ~]# cat break01.sh

for ((i=1; i<=9;i++))

for((j=1;j<=9;j++))

do

if [[ $j -le $i ]]; then #小于等于i時運算

let "Multi=$i*$j"

echo -n "$i*$j=$Multi "

else

break #j 一旦大于i則立即停止目前循環

fi

done

echo

[root@Cfhost-170820-UCNK ~]# sh break01.sh

1*1=1

2*1=2 2*2=4

3*1=3 3*2=6 3*3=9

4*1=4 4*2=8 4*3=12 4*4=16

5*1=5 5*2=10 5*3=15 5*4=20 5*5=25

6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36

7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49

8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64

9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

素數

#continue

[root@Cfhost-170820-UCNK ~]# cat continue_02.sh

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

for ((j=2;j<i;j++))

if !(($i%$j));then

continue 2

echo -n "$i "

[root@Cfhost-170820-UCNK ~]# sh continue_02.sh

1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97