1. 程式人生 > >Shell筆記7——while和until循環的應用實踐

Shell筆記7——while和until循環的應用實踐

linux 運維 shell腳本

本文主要講解Shell腳本開發中while和until循環的知識與實踐

基本大綱:

1.當型和直到型循環語法

2.當型和直到型循環的基本範例

3.shell腳本在後臺運行的知識

4.while循環按行讀文件的方式總結

5.while循環語句生產實踐




一:當型和直到型循環語法

循環語句命令常用於重復執行一條指令或一組指令,直到條件不再滿足時停止,Shell腳本語言的循環語句常見的有while、until、for、及select循環語句。

while循環語句主要用來重復執行一組命令或語句,在企業實際應用中,常用於守護進程或持續運行的程序。

1.while循環語句的基本語法為:

while <條件表達式>

do

指令...

done


註:while循環語句會對緊跟在while命令後的條件表達式進行判斷,如果該條件表達式成立,則執行while循環體裏的命令或語句(即語法中do和done之間的指令),每一次執行到done時就會重新判斷while條件表達式是否成立,知道條件表達式不成立時才會跳出while循環體。如果一開始條件表達式就不成立,那麽程序就不會進入循環體(即語法中do和done之間的部分)中執行命令了。


2.until循環語句的語法為:

until <條件表達式>

do

指令...

done


註:until循環語句的用法與while循環語句的用法類似,區別是until會在條件表達式不成立時,進入循環執行指令;條件表達式成立時,終止循環。




二:當型和直到型循環的基本範例

範例1:每隔5秒輸出一次系統負載(負載是系統性能的基礎重要指標)情況。

[root@aliyun xh]# cat while.sh 
#!/bin/bash
while true         #while true表示條件永遠為真,因此會一直運行,像死循環一樣,我們稱之為守護進程   
do
    uptime
    sleep 5        #休息5秒後繼續循環,控制循環的頻率
done
[root@aliyun xh]# sh while.sh 
 04:00:01 up 52 days,  5:44,  2 users,  load average: 0.64, 0.75, 1.83
 04:00:06 up 52 days,  5:45,  2 users,  load average: 0.67, 0.75, 1.83


範例2:將負載值追加到文件裏

[root@aliyun xh]# cat while.sh 
#!/bin/bash
while [ 1 ]        #註意[ ]裏面兩端要有空格,true和1都表示條件永遠成立
do
    uptime >>/tmp/uptime.log    #將負載值輸入到log文件裏
    usleep 5000000              #單位為微秒,其實就是5秒
done
[root@aliyun xh]# sh while.sh &
[1] 15782
[root@aliyun xh]# tailf /tmp/uptime.log         #使用tailf命令實時觀察輸出結果
 04:10:37 up 52 days,  5:55,  2 users,  load average: 0.60, 0.63, 1.22
 04:10:42 up 52 days,  5:55,  2 users,  load average: 0.63, 0.64, 1.22
 04:10:47 up 52 days,  5:55,  2 users,  load average: 0.90, 0.69, 1.23
 04:10:52 up 52 days,  5:55,  2 users,  load average: 0.91, 0.70, 1.23


範例3:計算從1加到100之和(用1+2+3+...+100的方法)

[root@aliyun xh]# cat jisuan.sh 
#!/bin/bash
i=1
sum=0
while ((i<=100))
do
    ((sum=sum+i))
    ((i++))
done
[ "$sum" -ne 0 ] && printf "totalsum is: ${sum}\n" 
[root@aliyun xh]# sh jisuan.sh 
totalsum is: 5050

法二:
[root@aliyun xh]# cat test2.sh 
#!/bin/bash
i=100
((sum=i*(i+1)/2))
printf "totalsum is: ${sum}\n


範例4:使用until命令實現循環豎向打印54321

[root@aliyun xh]# cat until.sh 
#!/bin/bash
i=5
until [[ $i < 1 ]]        #當條件表達式不成立時,進入循環執行命令。5<1不成立,所以進入循環
do
    echo $i
    ((i--))
done
[root@aliyun xh]# sh until.sh 
5
4
3
2
1





三:shell腳本在後臺運行的知識

sh while.sh & : 把腳本while.sh放到後臺執行(常用方法)

ctrl+c : 停止執行當前腳本或任務

ctrl+z : 暫停執行當前腳本或任務

bg : 把當前腳本或任務放到後臺執行

fg : 把當前腳本或任務放到前臺執行,如果有多個任務的話,可以使用fg加任務編號調出對應的腳本任務。如fg 2,是指調出第二個腳本任務

jobs : 查看當前執行的腳本或任務

kill : 關閉執行的腳本任務,即以“kill % 任務編號”的形式關閉腳本,這個任務編號,可以通過jobs來獲得


[root@aliyun xh]# sh while.sh &        #結尾使用&符號表示在後臺運行腳本
[1] 20994
[root@aliyun xh]# fg                   #執行fg命令將腳本放到前臺執行
sh while.sh
^Z                                     #臨時暫停執行腳本
[1]+  Stopped                 sh while.sh
[root@aliyun xh]# bg                   #將當前執行的腳本放到後臺運行
[1]+ sh while.sh &
[root@aliyun xh]# jobs                 #查看當前shell下運行的腳本任務
[1]+  Running                 sh while.sh &
[root@aliyun xh]# fg 1                 #可以使用fg加jobs輸出中的任務編號調出對應編號的腳本到前臺來運行
sh while.sh
^C                                     #當腳本在前臺運行時,可以執行ctrl+c快捷鍵停止腳本運行

#下面是使用kill命令關閉jobs任務腳本的示例
[root@aliyun xh]# jobs
[1]-  Running                 sh while.sh &
[2]+  Running                 sh while.sh &
[root@aliyun xh]# kill %2                    #註意任務編號的寫法
[root@aliyun xh]# jobs   
[1]-  Running                 sh while.sh &
[2]+  Terminated              sh while.sh    #表示腳本已關閉




四:while循環按行讀文件的方式總結

方式1:采用exec讀取文件,然後進入while循環處理

exec < file
sum=0
while read line
do    
    cmd
done


方式2:使用cat讀取文件內容,然後通過管道進入while循環處理

cat file_path | while read line
do
    cmd
done


方式3:在while循環結尾done處通過輸入重定向指定讀取的文件

while read line
do    
    cmd
done < file


範例:開發一個Shell腳本實現linux系統命令cat讀文件的基本功能

[root@aliyun xh]# cat cat.sh 
#!/bin/bash
while read line
do
    echo $line
done < $1
[root@aliyun xh]# sh cat.sh nohup.out 
03:44:15 up 52 days, 5:29, 2 users, load average: 0.74, 2.47, 3.93
03:44:17 up 52 days, 5:29, 2 users, load average: 0.68, 2.43, 3.91
03:44:19 up 52 days, 5:29, 2 users, load average: 0.68, 2.43, 3.91




五:while循環語句生產實踐

範例:使用while守護進程的方式監控網站,每隔10秒確定一次網站是否正常。

[root@aliyun xh]# cat whilesc.sh 
#!/bin/bash
. /etc/init.d/functions        #引入函數庫
if [ $# -ne 1 ]                #判斷腳本參數
then
    echo $"usage $o URL"
    exit 1
fi
while true
do
    if [ `curl -o /dev/null --connect-timeout 5 -s -w "%{http_code}" $1|egrep -w "200|301|302" | wc -l` -ne 1 ]        #判斷http狀態值
    then
        action "$1 is error"  /bin/false        #優雅顯示
    else
        action "$1 is ok" /bin/true
    fi
    sleep 10
done
[root@aliyun xh]# sh whilesc.sh baidu.com
baidu.com is ok                                            [  OK  ]
baidu.com is ok                                            [  OK  ]
[root@aliyun xh]# sh whilesc.sh baidusds.com
baidusds.com is error                                      [FAILED]
baidusds.com is error                                      [FAILED]























Shell筆記7——while和until循環的應用實踐