linux脚本之循环语句:for、while、until
一、for循环
1、in 后面是常量,空格分隔
#!/bin/sh for str in aa bb cc dd do echo "str is "$str done
2、in 后面是变量,空格分隔
#!/bin/sh strlist="aa bb cc dd ee" for str in $strlist do echo "str is "$str done
3、从文件中取,空格或换行分隔
#!/bin/sh for str in $(cat test.log) do echo "str is "$str done
4、没有in的for循环,变量和判断语句在括号中
#!/bin/sh s=0 for ((i=1;i<=100;i=i+1)) do s=$(($s+$i)) done echo "1+2+3+...+100 is:"$s
二、while循环
#!/bin/sh s=0 i=1 while [ "$i" -le 100 ] do s=$(($s+$i)) i=$(($i+1)) done echo "1+2+3+...+100 sum is: "$s
break或exit退出
#!/bin/sh i=0 while true do i=$(($i+1)) if [ "$i" -gt 10 ];then break(或exit) fi echo "i:"$i done
三、until循环
#!/bin/sh s=0 i=1 until [ "$i" -gt 100 ] do s=$(($s+$i)) i=$(($i+1)) done echo "1+2+3+...+100 sum is: "$s