shell循环loop
1. 循环
2. while循环
#!/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
3. until循环
#!/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]$
4. for固定循环
#!/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
5. for数值循环
#!/bin/bash # desc : for loop sum=0 for (( i=0; i<=100; i++ )) do sum=$(($sum+$i)) done echo "sum is : " $sum
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。