A concise tutorial on shell script break and Continue commands _linux shell

Source: Internet
Author: User

During the loop, there are times when you need to force out of the loop without reaching the end of the loop, and the shell uses two commands to implement the function: Break and continue.

Break command

The break command allows you to jump out of all loops (terminating all loops after execution).

In the following example, the script goes into a dead loop until the user enters a number greater than 5. To jump out of this loop, return to the shell prompt with the break command.

Copy Code code as follows:

#!/bin/bash
While:
Todo
Echo-n "Input a number between 1 to 5:"
Read Anum
Case $aNum in
1|2|3|4|5) echo "Your number is $aNum!"
;;
*) echo "You don't select a number between 1 to 5, game is over!"
Break
;;
Esac
Done

Continue

The continue command is similar to the break command, except that it does not jump out of all loops and simply jumps out of the current loop.

Modify the example above to:

Copy Code code as follows:

#!/bin/bash
While:
Todo
Echo-n "Input a number between 1 to 5:"
Read Anum
Case $aNum in
1|2|3|4|5) echo "Your number is $aNum!"
;;
* echo "You don't select a number between 1 to 5!"
Continue
echo "Game is over!"
;;
Esac
Done

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

Copy Code code as follows:

echo "Game is over!"

Will never be executed.

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.