shell腳本編程學習筆記-while循環
While使用的不多,一般守護進程程序或始終循環執行會用,其他循環運算都用for代替。
1.1 當型和直到型循環語法
(1)while條件語句
語法:
While 條件
do
指令….
done
手機充值:發短信扣費,充值100,每次扣1角5,當費用低於1角5分就不能發了。
(2)until條件語句
語法:
until 條件
do
指令…
Done
提示:只循環一次,應用場景不多,了解就好。
1.2 當型和直到型循環基本範例
休息命令:sleep休息1秒,usleep1000000休息1秒。達到一分鐘就用定時任務。
1.2.1 範例1:每隔2秒記錄一次系統負載情況
方法一:每隔2秒屏幕輸出負載值
[root@shellbiancheng jiaobenlianxi]# cat while1.sh
#!/bin/sh
while true
do
uptime
sleep 2
done
提示:while true 表示條件永遠為真,因此會一直執行,像死循環一樣,我們稱之為守護進程。
[root@shellbiancheng jiaobenlianxi]# sh while1.sh 02:50:13 up 1 day, 11:47, 3 users, load average: 0.00, 0.00, 0.00 02:50:15 up 1 day, 11:47, 3 users, load average: 0.00, 0.00, 0.00
方法二:追加到log日誌文件裏
[root@shellbiancheng jiaobenlianxi]# cat while2.sh
#!/bin/sh
while [ 1 ]
do
uptime >>./uptime.log
sleep 2
done
提示:
[root@shellbiancheng jiaobenlianxi]# sh while2.sh &
[1] 3991
在後臺永久執行,我們稱之為守護進程模式。
[root@shellbiancheng jiaobenlianxi]# tail -f uptime.log 03:00:24 up 1 day, 11:57, 3 users, load average: 0.01, 0.01, 0.00 03:00:26 up 1 day, 11:57, 3 users, load average: 0.01, 0.01, 0.00
防止客戶端執行腳本中斷的方法
(1)sh while2.sh & 加&符號
(2)nohup while2.sh &
(3)screen 保持會話
1.2.2 腳本在後臺執行知識擴展
擴展資料:
bg: 後臺運行
fg: 掛起程序
jobs: 顯示後臺程序
kill,killall,pkill: 殺掉進程
crontab:設置定時
ps: 查看進程
pstree: 顯示進程
nice: 改變優先級
nohup:用戶退出系統之後繼續工作
pgrep:查找匹配條件的進程
strace:跟蹤一個進程的系統調用情況,如果在工作中某個進程使用率過高可以用strace查看進程系統調用情況,如果看不懂可以讓開發去看。
Itrace:跟蹤進程調用庫函數的情況。
vmstat:報告虛擬內存統計信息。
1.3 簡單範例
1.3.1 範例1:通過while語句計算從1加到100的和,請用1+2+3的方法
[root@shellbiancheng jiaobenlianxi]# cat 1-100.sh
#!/bin/sh
sum=0
i=1
while [ $i -le 100 ]
do
((sum=sum+i))
((i++))
done
echo $sum
1.3.2 範例2:下面通過數學公式計算的結果
[root@shellbiancheng jiaobenlianxi]# cat sum1-100.sh
#!/bin/sh
i=100
((sum=i*(i+1)/2))
echo $sum
更多實現1到100之和請大家閱讀老男孩老師的博文。
http://blog.51cto.com/oldboy/767862
1.3.3 範例3:
手機充值10元,每發一次短信(輸出當前余額)花費1角5分錢,當余額低於1角5分錢不能發短信,提示余額不足,請充值(可以允許用戶繼續充值繼續發短信),請用while語句實現。
提示:單位換算,統一單位,統一成整數。10元=1000分,1角5分=15分
[root@shellbiancheng jiaobenlianxi]# cat huafei.sh
#!/bin/bash
HUAFEI=100
YUE=25
if [ -f /etc/init.d/functions ];then
. /etc/init.d/functions
fi
OPTION() {
case "$option" in
[yY]|[yY][eE][sS])
echo "Send a success"
echo $txt >>/var/log/consum.log
((HUAFEI=HUAFEI-YUE))
echo "You‘re still saving money $HUAFEI"
;;
[nN]|[nN][oO])
echo "Abort send, succeed."
;;
*)
echo "Input error"
esac
return 0
}
CHANGE1() {
expr $change + 1 &>/dev/null
if [ "$?" -ne "0" -a "$change" != "-1" ];then
echo "There are illegal characters, please reenter the amount of recharge."
else
break
fi
return 0
}
CHANGE() {
while true
do
read -p "Please input the amount you want to recharge:" change
CHANGE1
done
return 0
}
CHANGE2() {
((HUAFEI+=change))
echo "You‘re still saving money $HUAFEI"
}
OPTION2() {
case "$option2" in
[yY]|[yY][eE][sS])
CHANGE
CHANGE2
;;
[nN]|[nN][oO])
exit 1
;;
*)
echo "Input error, please enter the correct amount."
CHANGE
CHANGE2
esac
return 0
}
linzhongniao() {
if [ "$HUAFEI" -lt "$YUE" ];then
read -p "The balance is insufficient, please recharge[y|n]" option2
OPTION2
fi
return 0
}
main() {
while [ "$HUAFEI" -ge "$YUE" ]
do
read -p "Please enter the content of the text message:" txt
read -p "Confirm send [y|n]" option
OPTION
linzhongniao
done
return 0
}
main
1.4 擴展
While按行讀文件的方式
1.4.1 方法一
[root@shellbiancheng jiaobenlianxi]# cat while_duwenjian1.sh
#!/bin/bash
exec <nginx.sh
sum=0
while read line
do
cmd
done
1.4.2 方法二
[root@shellbiancheng jiaobenlianxi]# cat while_duwenjian2.sh
#!/bin/bash
cat ${FILE_PATH}|while read line
do
cmd
done
1.4.3 方法三
[root@shellbiancheng jiaobenlianxi]# cat while_duwenjian3.sh
#!/bin/bash
while read line
do
cmd
done<FILE
1.4.4 問題分析apache日誌例子
計算apache一天的日誌access_2010-12-8.log中所有行的日誌元素的訪問字節數的總和。綜合實現程序。練習日誌:見目錄下access_2012-12-8.log,也可以用自己的apache日誌,請用while語句實現。
[root@shellbiancheng jiaobenlianxi]# cat while3.sh
#!/bin/bash
exec < /etc/httpd/logs/access_log
while read line
do
i=echo $line|awk ‘{print $10}‘
expr $i + 1 &>/dev/null
if [ $? -ne 0 ];then
continue
fi
((sum+=1))
done
[ -n "$sum" ] && echo $sum
2.While循環小結
a.While循環的特長是執行守護進程以及我們希望循環不退出持續執行的情況,用於頻率小於一分鐘循環處理(crond),其他的while循環幾乎都可以被for循環替代。
b.case語句可以替換if語句,一般在系統啟動腳本傳入少量固定規則字符串,用case語句。其他普通判斷多用if語句。
c.if和for語句最常用,其次是while(守護進程),case(服務啟動腳本)。
各個語句的應用場景:
條件表達式,簡單的判斷(文件是否存在,字符串是否為空等)。
if取值判斷,不同值數量較少的情況。
for正常的循環處理,最常用!
while守護進程、無限循環(sleep)。
case服務啟動腳本,菜單。
函數邏輯清晰,減少重復語句。
shell腳本編程學習筆記-while循環