1. 程式人生 > >for循環、while循環、continue、break、exit解析、select用法

for循環、while循環、continue、break、exit解析、select用法

for循環、while循環、contin

20.10 for循環

eg:

求1到100數字的和。
[root@localhost sbin]# vim sum.sh
#!/bin/bash
sum=0
for i in seq 1 5
do
sum=$[sum+$i]
done
echo "$sum"

[root@localhost sbin]# sh sum.sh
15
文件列表循環
[root@localhost sbin]# vim for.sh
#!/bin/bash
dir=/usr/local/sbin/
for a in ls $dir
do
if [ -d $a ]
then
echo $a
ls $a
fi
done
echo "No directory file!"

[root@localhost sbin]# sh -x for.sh

  • dir=/usr/local/sbin/
    ++ ls /usr/local/sbin/
  • for a in ‘ls $dir
  • ‘[‘ -d datefile.sh ‘]‘
  • for a in ‘ls $dir
  • ‘[‘ -d filetar.sh ‘]‘
  • for a in ‘ls $dir
  • ‘[‘ -d for.sh ‘]‘
  • for a in ‘ls $dir
  • ‘[‘ -d if2.sh ‘]‘
  • for a in ‘ls $dir
  • ‘[‘ -d sum.sh ‘]‘
  • echo ‘No directory file!‘
    No directory file!
    for——分隔符
    [root@localhost adai]# ll
    總用量 0
    -rw-r--r-- 1 root root 0 9月 14 19:25 1
    -rw-r--r-- 1 root root 0 9月 14 19:25 2
    -rw-r--r-- 1 root root 0 9月 14 19:25 3 4.txt
    #註意:3 4.txt是一個文件(34中間有一個空格)

[root@localhost adai]# for i in ls ./; do echo $i ; done
1
2
3
4.txt
以上結果顯示說明,for默認情況下把空格或換行符(回車)作為分隔符。

20.11-20.12 while循環

格式: while 條件;do…;done

eg:

當系統負載大於10的時候,發送郵件,每隔30秒執行一次。
[root@localhost adai]# vim while.sh
#!/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
#while “:”表示死循環,也可以寫成while true,意思是“真”(數學--真命題、假命題)

#Attention:awk -F ‘load average: ‘此處指定‘load average: ‘為分隔符,註意冒號後面的空格
#如果不加該空格,過濾出來的結果會帶空格,需要在此將空格過濾掉

[root@localhost adai]# sh -x while.sh

  • :
    ++ head -1
    ++ awk -F ‘load average: ‘ ‘{print $2}‘
    ++ cut -d. -f1
    ++ w
  • load=0
  • ‘[‘ 0 -gt 10 ‘]‘
  • sleep 30
    .
    .
    .
    如果不手動停止該腳本,它會一直循環執行(按Ctrl+c結束),實際環境中配合screen使用。

交互模式下,用戶輸入一個字符,檢測該字符是否符合條件,如:空、非數字、數字。分別對字符做出判斷,然後做出不同的回應。
[root@localhost sbin]# vim while2.sh
#!/bin/bash
while true
do
read -p "Please input a number:" n
if [ -z "$n" ]
then
echo "You need input some characters!"
continue
fi
n1=echo $n|sed ‘s/[-0-9]//g‘
if [ -n "$n1" ]
then
echo "The character must be a number!"
continue
fi
break
done
echo $n
#continue:中斷本次while循環後重新開始;
#break:表示跳出本層循環,即該while循環結束

[root@localhost sbin]# sh while2.sh
Please input a number:
You need input a character!
Please input a number:eee333
The character must be a number!
Please input a number:3
3
20.13 break 跳出循環

eg:

[root@localhost sbin]# vim break.sh
#!/bin/bash
for i in seq 1 5
do
echo "$i"
if [ $i -eq 3 ]
then
break
fi
echo "$i"
done
echo "Finished!"

[root@localhost sbin]# sh break.sh
1
1
2
2
3
Finished!
即,跳出while循環,繼續執行循壞之外的命令。

20.14 continue 結束本次循環

eg:

[root@localhost sbin]# vim continue.sh
#!/bin/bash
for i in seq 1 5
do
echo "$i"
if [ $i -eq 3 ]
then
continue
fi
echo "$i"
done
echo "Finished!"

[root@localhost sbin]# sh continue.sh
1
1
2
2
3
4
4
5
5
Finished!
即,結束本次循環之後重新開始下一次循環。

20.15 exit退出整個腳本

eg:

[root@localhost sbin]# vim exit.sh
#!/bin/bash
for i in seq 1 5
do
echo "$i"
if [ $i -eq 3 ]
then
exit
fi
echo "$i"
done

[root@localhost sbin]# sh exit.sh
1
1
2
2
3
退出整個腳本,後面的命令不會執行。

擴展:shell中select的用法

select也是循環的一種,它比較適合用在用戶選擇的情況下。
比如,我們有一個這樣的需求,運行腳本後,讓用戶去選擇數字,選擇1,會運行w命令,選擇2運行top命令,選擇3運行free命令,選擇4退出。腳本這樣實現:

#!/bin/bash

echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
echo
select command in w top free quit
do
case $command in
w)
w
;;
top)
top
;;
free)
free
;;
quit)
exit
;;
*)
echo "Please input a number:(1-4)."
;;
esac
done
執行結果如下:

sh select.sh
Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit

1) w
2) top
3) free
4) quit
#? 1
16:06:58 up 109 days, 22:01, 1 user, load average: 0.11, 0.05, 0.01
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 222.128.156.84 16:05 0.00s 0.00s 0.00s w

#? 3
total used free shared buffers cached
Mem: 1020328 943736 76592 0 86840 263624
-/+ buffers/cache: 593272 427056
Swap: 2097144 44196 2052948
#?
我們發現,select會默認把序號對應的命令列出來,每次輸入一個數字,則會執行相應的命令,命令執行完後並不會退出腳本。它還會繼續讓我們再次輸如序號。序號前面的提示符,我們也是可以修改的,利用變量PS3即可,再次修改腳本如下:

#!/bin/bash
PS3="Please select a number: "
echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
echo

select command in w top free quit
do
case $command in
w)
w
;;
top)
top
;;
free)
free
;;
quit)
exit
;;
*)
echo "Please input a number:(1-4)."
esac
done
如果想要腳本每次輸入一個序號後就自動退出,則需要再次更改腳本如下:

#!/bin/bash
PS3="Please select a number: "
echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
echo

select command in w top free quit
do
case $command in
w)
w;exit
;;
top)
top;exit
;;
free)
free;exit
;;
quit)
exit
;;
*)
echo "Please input a number:(1-4).";exit
esac
done

for循環、while循環、continue、break、exit解析、select用法