重击:在For/While循环中继续
时间:2020-01-09 10:45:55 来源:igfitidea点击:
在UNIX或者Linux操作系统下,如何在Bash中的for或者while循环中继续?
Continue语句用于恢复封闭的FOR,WHILE或者UNTIL循环的下一个迭代。
Bash for Loop继续语法
for i in something do [ condition ] && continue cmd1 cmd2 done
一个示例shell脚本,用于打印1到6之间的数字,但使用for循环跳过打印3和6的数字:
#!/bin/bash
for i in 1 2 3 4 5 6
do
### just skip printing $i; if it is 3 or 6 ###
if [ $i -eq 3 -o $i -eq 6 ]
then
continue ### resumes iteration of an enclosing for loop ###
fi
# print $i
echo "$i"
done
Bash而循环继续语法
while true do [ condition1 ] && continue cmd1 cmd2 done
一个示例shell脚本,用于打印1到6之间的数字,但使用while循环跳过打印3和6的数字:
#!/bin/bash
i=0
while [ $i -lt 6 ]
do
(( i++ ))
### resumes iteration of an enclosing while loop if $i is 3 or 6 ###
[ $i -eq 3 -o $i -eq 6 ] && continue
echo "$i"
done

