shell脚本:嵌套循环并继续

use*_*686 5 shell continue

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

  if [ "$x" > 3 ];then
    continue
  fi
done
Run Code Online (Sandbox Code Playgroud)

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

有没有更简单的方法让我可以使用类似的方法continue 2来逃避外循环?

Ed *_*ton 1

“break”和“Continue”是“goto”的近亲,通常应该避免,因为它们引入了一些无名的条件,导致程序控制流的跳跃。如果存在一个条件,需要跳转到程序的其他部分,那么下一个阅读它的人会感谢您为该条件命名,这样他们就不必弄清楚它!

对于您的情况,您的脚本可以更简洁地编写为:

dataInRange=1
while [condition -a $dataInRange]
do
  for [condition -a $dataInRange]
  do
    if [ "$x" > 3 ];then
      dataInRange=0
    fi
  done
done
Run Code Online (Sandbox Code Playgroud)