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