Shell Scripting Learning 21 shell break and Continue commands

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.

  1. #!/bin/bash
  2. While :
  3. Do
  4. echo- n "Input a number between 1 to 5:"
  5. Read Anum
  6. case $aNum in
  7. 1| 2| 3| 4| 5) echo "Your number is $aNum!"
  8. ;;
  9. *) echo "You don't select a number between 1 to 5, game is over!"
  10. Break
  11. ;;
  12. Esac
  13. 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. For example:

    1. 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:

  1. #!/bin/bash
  2. For var1 in 1 2 3
  3. Do
  4. For var2 in 0 5
  5. Do
  6. if [ $var 1 -eq 2-a $var 2 -eq 0 ] /c0>
  7. Then
  8. break 2
  9. Else
  10. Echo "$var 1 $var 2"
  11. fi
  12. Done
  13. Done

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:

  1. #!/bin/bash
  2. While :
  3. Do
  4. echo- n "Input a number between 1 to 5:"
  5. Read Anum
  6. case $aNum in
  7. 1| 2| 3| 4| 5) echo "Your number is $aNum!"
  8. ;;
  9. *) echo "You don't select a number between 1 to 5!"
  10. Continue
  11. Echo "Game is over!"
  12. ;;
  13. Esac
  14. Done

Running code discovery, when you enter a number greater than 5, the loop in this example does not end, and the statement

    1. 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:

  1. #!/bin/bash
  2. NUMS="1 2 3 4 5 6 7"
  3. For NUM in $NUMS
  4. Do
  5. Q= ' expr $NUM % 2 '
  6. if [ $Q -eq 0 ]
  7. Then
  8. echo "Number is an even number!!"
  9. Continue
  10. fi
  11. echo "Found odd number"
  12. 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 Scripting Learning 21 shell break and Continue commands

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.