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

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

In the above script I have to test "$x" > 3 twice. Actually the first time I test it, if it is true I want to escape the while loop and continue to the next while loop.

Is there any simpler way so I can use something like continue 2 to escape the outer loop?

Neil
  • 54,642
  • 8
  • 60
  • 72
user1769686
  • 505
  • 2
  • 10
  • 16

1 Answers1

1

"break" and "continue" are close relatives of "goto" and should generally be avoided as they introduce some nameless condition that causes a leap in the control flow of your program. If a condition exists that makes it necessary to jump to some other part of your program, the next person to read it will thank you for giving that condition a name so they don't have to figure it out!

In your case, your script can be written more succinctly as:

dataInRange=1
while [condition -a $dataInRange]
do
  for [condition -a $dataInRange]
  do
    if [ "$x" > 3 ];then
      dataInRange=0
    fi
  done
done
Ed Morton
  • 188,023
  • 17
  • 78
  • 185