腳本編程:
順序結構選擇結構ifcase 循環結構
for
while
untilwhile循環:適合循環次數未知的場景,一定要有退出條件
while
進入循環:條件滿足
退出循環:條件不滿足
語法:
while CONDITON;do
statement
…
done
-+
計算100以內所有整數的合
#!/bin/bash
#
declare -i I=1
declare -i SUM=0
while [ $I -le 100];do
let SUM+=$I
let I+=1
done
echo"$SUM"
練習2:寫一個腳本,用戶輸入任何字母都變成大寫,輸入quit退出
#!/bin/bash
#
read -p "Inputsometing:" STRING
while [ $STRING !='quit' ];do
echo $STRING |tr'a-z' 'A-Z'
read -p "Inputsometing:" STRING
done
練習3.寫一個腳本每隔5秒鐘檢測一下admin用戶是否登錄。
#!/bin/bash
#
who |grep admin&>/dev/null
RETVAL=$?
while [ $RETVAL -ne0 ];do
echo "`date`,admin is not log."
sleep 5
who |grep admin&>/dev/null
RETVAL=$?
done
echo "admin islogged in."
until
進入循環:條件不滿足
退出循環:條件滿足
until CONDITION;do
statement
…
done
練習,寫一個腳本,當用戶輸入quit時退出,輸入小寫字母時自動變成大寫字母
#!/bin/bash
#
read -p"shuru": R
until [ $R = 'quit'];do
echo $R |tr 'a-z''A-Z'
read -p"shuru": R
done
練習,寫一個腳本,每隔五秒鐘判斷admin是否登錄
#!/bin/bash
#
who|grep admin&>/dev/null
R=$?
until [ $R -eq 0];do
echo"`date`,admin not come"
sleep 5
who|grep admin&>/dev/null
R=$?
done
echo “admin loggedin.”
本文出自 “linux運維” 博客,謝絕轉載!