Shell script: Exiting the loop

Source: Internet
Author: User

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

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.