Article Title: When will shell scripts exit abnormally. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Shell will exit the script only when an error is encountered and exit is executed. Otherwise, it will continue to be executed. If there is a loop, it will finish the loop and continue.
So what errors will shell jump out of execution?
In my understanding, shell will definitely exit when a syntax error is encountered, but there is no syntax error, but there is a wrong execution error, shell won't exit (here is a situation:
Cd/app/data/download
Rm-rf *
If such a command exists in the script, we assume that the download folder does not exist in/app/data/. Although an error is reported when executing this command, the script will not exit, instead, it continues to execute the Terrible "rm-rf *" command. Which path does the script run under? If it is in the root directory, the system will be down. Not only will the system be down, but even the data will be lost. How terrible is this !)
Simply put, when we use
Sh/ksh-n filename. sh
If there is no error, the shell will not jump out because of an error. The exit condition is either to execute exit or to execute the complete script.
After a complete script is executed, shell executes exit by default and the return value is 0.
Note that the return value after the shell script is executed is $? The value is determined by whether the last command in the shell script is successfully executed.
In
If []; then
...
Else
...
Fi
In such a script, if the judgment condition fails and the content after else is executed, the script will not exit if exit is not encountered, but will continue to be executed.
Therefore, we need to figure out whether to exit. This is determined by the business logic!
When a script contains many commands and other scripts, are there any of these two situations:
1. Execute the script from start to end with one command, and exit after the script is executed.
2. The script is quickly exited during execution, but the script task is not completed immediately, but is still executed in the system.
This is only related to whether or not the command or other scripts in the script are executed in the background. If so, the parent process executing the command or script is not the script but under the system init.