The shell also uses break and continue to jump out of the loop.
Break command
In the following example, the script enters a dead loop until the user enters a number greater than 5, using break to jump out of the loop.
#!/bin/Bash while : Do Echo-N"Input A number between 1 to 5:"Read Anum Case$aNuminch 1|2|3|4|5)Echo "Your number is $aNum!" ;; *)Echo "You does not select a number between 1 to 5, the game is over!"Break ;; Esac Done
In a nested loop, the break command can also be followed by an integer that indicates a loop that jumps out of the first layer.
Break n
Here is an example of a nested loop, if Var1 equals 2, and var2 equals 0, jump out of the loop:
#!/bin/Bash forVar1inch 1 2 3 Do forVar2inch 0 5 Do if[$var 1-eq2-A $var 2-eq0 ] Then Break2 Else Echo "$var 1 $var 2" fi Done Done
Continue command
The continue command is similar to the break command, with only a little difference, and it does not jump out of all loops and just jumps out of the current loop.
Similarly, the continue can be followed by a number, indicating that the first layer jumps out of the loop.
Shell script: Exiting the loop