The break,continue of the shell script, and the exit difference

Source: Internet
Author: User

Break in a Linux script continue exit return

Break
End and Exit loops

Continue
The following code is not executed in the loop and continue into the next round loop

Exit
Exit the script,
Always bring an integer to the system, such as Exit 0

Return
Returning data to a function
Or a script that returns a result to the calling function

I understand: break is jumping out of the loop immediately, continue is jumping out of the current condition loop, continuing the next round of conditional loops; exit is to exit the entire script directly.

For example:

In the loop, sometimes you need to force out of the loop when the loop end condition is not reached, 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 (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, you need to use the break command.

Copy CodeThe code is as follows:
#!/bin/bash
While:
Do
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, 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:

The code is as follows:
#!/bin/bash
While:
Do
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 discovery, when you enter a number greater than 5, the loop in this example does not end, and the statement

The code is as follows:
echo "Game is over!"


Will never be executed.

The break,continue of the shell script, and the exit difference

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.