Shell Learning 20-shell jumping out of the loop

Source: Internet
Author: User

In the loop, sometimes you need to force out of the loop when the loop end condition is not reached, like most programming languages, the shell also uses break and continue to jump out of the loop.
Break Command
The break command allows you to jump out of all loops (all loops after the execution is terminated).
In the following example, the script enters a dead loop until the user enters a number greater than 5. To jump out of this loop and return to the shell prompt, use the break command.
#!/bin/bashwhile:d oecho-n "Input a number between 1 to 5:" Read anumcase $aNum in1|2|3|4|5) echo "Your number is $aNum!" ;; *) echo "You don't select a number between 1 to 5, game is over!" break;; Esacdone
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. For example:
Break n
Indicates a jump out of the nth layer loop.
Here is an example of a nested loop, if Var1 equals 2, and var2 equals 0, jump out of the loop:
#!/bin/bashfor var1 in 1 2 3dofor var2 in 0 5doif [$var 1-eq 2-a $var 2-eq 0]thenbreak 2elseecho "$var 1 $var 2" Fidonedon E
As above, break 2 means to jump out of the outer loop directly. Operation Result:
1 01 5
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.
To modify the above example:
#!/bin/bashwhile:d oecho-n "Input a number between 1 to 5:" Read anumcase $aNum in1|2|3|4|5) echo "Your number is $aNum!" ;; *) echo "You don't select a number between 1 to 5!" Continueecho "Game is over!";; Esacdone
Running code discovery, when you enter a number greater than 5, the loop in this example does not end, and the statement
echo "Game is over!"
Will never be executed.
Similarly, the continue can be followed by a number, indicating that the first layer jumps out of the loop.
Let's look at a continue example:
#!/bin/bashnums= "1 2 3 4 5 6 7" for NUM in $NUMSdoQ = ' expr $NUM% 2 ' if [$Q-eq 0]thenecho "number is an even number!!" Continuefiecho "Found odd number" done
Operation Result:
Found Odd Numbernumber is an even number!! Found Odd Numbernumber is an even number!! Found Odd Numbernumber is an even number!! Found Odd number

Shell Learning 20-shell jumping out of the loop

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.