SHELL實戰day8

                                  一    while循環

語法 while 條件; do … ; done

案例1
#!/bin/bash
while :(:表示死循環)
do
    load=w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1
    if [ $load -gt 10 ]
    then
        top|mail -s "load is high: $load" [email protected]
    fi
    sleep 30
done

案例2
#!/bin/bash
while :
do
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "you need input sth."
continue
fi
n1=echo $n|sed 's/[0-9]//g'
if [ -n "$n1" ]
then
echo "you just only input numbers."
continue
fi
break
done
echo $n

                                       二 break跳出循環

#!/bin/bash
for i in seq 1 5
do
    echo $i
    if [ $i == 3 ]
(當比較值爲字符串的時候,需要使用==)
    then
        break
    fi
    echo $i
done
echo aaaaaaa

                                  三 continue結束本次循環

忽略continue之下的代碼,直接進行下一次循環
#!/bin/bash
for i in seq 1 5
do
    echo $i
    if [ $i == 3 ]
    then
        continue
    fi
    echo $i
done
echo $i

                                四 exit退出整個腳本

#!/bin/bash
for i in seq 1 5
do
    echo $i
    if [ $i == 3 ]
    then
        exit
    fi
    echo $i
done
echo aaaaaaa

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章