#!/bin/bash # desc : while loop while [ "$yn" != "yes" -a "$yn" != "YES" ] do read -p "Please input yes/YES to stop : " yn done echo "OK"执行结果:
[work@www sh]$ sh while.sh Please input yes/YES to stop : no Please input yes/YES to stop : yes OK [work@www sh]$例:从1加到100
#!/bin/bash # desc : while loop sum=0 i=0 while [ $i -lt 100 ] do i=$(($i+1)); sum=$(($sum+$i)) done echo "From 1 to 100, sum is : " $sum
#!/bin/bash # desc : while loop until [ "$yn" == "yes" -o "$yn" == "YES" ] do read -p "Please input yes/YES to stop : " yn done echo "OK"执行:
[work@www sh]$ sh while.sh Please input yes/YES to stop : hi Please input yes/YES to stop : yes OK [work@www sh]$
#!/bin/bash # desc : for loop for animal in dog cat pig do echo "HI, ${animal}" done执行:
[work@www sh]$ sh for.sh HI, dog HI, cat HI, pig [work@www sh]$例:当前目录文件
#!/bin/bash # desc : for loop filelist=$(ls) for filename in $filelist do echo $filename done
#!/bin/bash # desc : for loop sum=0 for (( i=0; i<=100; i++ )) do sum=$(($sum+$i)) done echo "sum is : " $sum
原文地址:http://blog.csdn.net/yonggang7/article/details/40679701