語法:for 變量名 in 條件; do …; done
for 會以空格或者回車爲分隔符
案例1bash
#!/bin/bash sum=0 for i in `seq 1 100` do sum=$[$sum+$i] echo $i done echo $sum
文件列表循環ide
#!/bin/bash cd /etc/ for a in `ls /etc/` do if [ -d $a ] then ls -d $a fi done
語法 while 條件; do … ; donecode
#!/bin/bash while true do load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1` if [ $load -gt 10 ] then /usr/lib/zabbix/alertscripts/mail.py chinantfy@163.com "load is high" "load is high: $load" fi sleep 30 done
#!/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
#!/bin/bash for i in `seq 1 5` do echo $i if [ $i == 3 ] then break fi echo $i done echo aaaaaaa
忽略continue之下的代碼,直接進行下一次循環blog
#!/bin/bash for i in `seq 1 5` do echo $i if [ $i == 3 ] then continue fi echo $i done echo $i
#!/bin/bash for i in `seq 1 5` do echo $i if [ $i == 3 ] then exit fi echo $i done echo aaaaaaa