shell脚本:嵌套循环并继续

问题描述:

while [condition] 
do 
    for [condition] 
    do 
    if [ "$x" > 3 ];then 
     break 
    fi 
    done 

    if [ "$x" > 3 ];then 
    continue 
    fi 
done 

在上面的脚本中,我必须测试"$x" > 3两次。实际上,我第一次测试它,如果它是真的我想逃避while循环并继续下一个while循环。shell脚本:嵌套循环并继续

有没有更简单的方法,所以我可以使用像continue 2这样的东西来逃避外层循环?

+2

为什么不使用'继续2'在内部循环? –

+0

标签和转到? :) – alex

+0

是否有继续2? – user1769686

“break”和“continue”是“goto”的近亲,通常应该避免,因为它们引入了一些导致程序控制流程出现跳跃的无名条件。如果有条件需要跳转到程序的其他部分,下一个阅读它的人会感谢你给这个条件起一个名字,所以他们不必弄明白!

在你的情况,你的脚本可以写得更简洁为:

dataInRange=1 
while [condition -a $dataInRange] 
do 
    for [condition -a $dataInRange] 
    do 
    if [ "$x" > 3 ];then 
     dataInRange=0 
    fi 
    done 
done