Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of exit 0 Pit in Shell

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "Case Analysis of Exit 0 Pit in Shell". The explanation content in this article is simple and clear, and it is easy to learn and understand. Please follow the idea of Xiaobian to study and learn "Case Analysis of Exit 0 Pit in Shell" together.

In shell programming, exit 0!

First of all, it must be made clear that exit 0 is not return 0. The difference is that return 0 is used in functions, indicating that the function is successfully executed and returns 0; exit 0 means that your current program is successfully executed and will directly exit the currently executed script or program. Conversely, return -1 generally indicates that the function fails to execute and returns an error; exit 1 (or greater than 1) indicates that the program fails to execute and exits the program. exit is used to return the state at the end of the process, usually using $? in the parent shell. Variable to obtain the exit status of the subshell and determine whether the subshell was successfully executed.

Although the difference between the two is obvious, it is easy to understand why it is necessary to make a record? An error did occur when a function interface using exit 0 and exit 1 was supposed to be executed in a child shell, and the parent shell returned { $?} to its process. } to make a judgment operation, the result is that someone directly added the function interface to the environment script to make a judgment, the result directly led to the failure of the environment deployment, obviously, tragedy... Haha, so I thought I'd write a blog and record it

Note: Use return in interface functions, exit when you need to exit the execution (subshell) program, for example

#/bin/bashfunction check_extern_input(){ read param if [ "${param}" == "exit 0" ] then echo "successful exit process right now! " exit 0 elif [ "${param}" == "exit 1" ] then echo "failed exit process right now! " exit 1 elif [ "${param}" == "return 0" ] then echo "func normal return, process not exit, Enter go on... " return 0 else echo "Input again:" fi }while true do check_extern_inputdone

Before using unfamiliar shell commands or syntax, it is best to write a script to verify and use it, forming a good habit of self-test!

ps: Meaning of exit0 and exit1 in shell scripts

exit 0: Run the program normally and exit the program;

exit 1: abnormal operation leads to exit of the program;

Exit 0 tells users of your program that your program ended normally. If exit is non-zero, users of your program will generally assume that

Your program made an error.

After invoking your program in the shell, use echo $? Command to see the exit value of your program. In shell scripts, you usually write scripts based on

The last command? Value to perform some process control. 0 represents the correct execution of the program, as shown in the following example:

Thank you for reading, the above is the content of "Shell exit 0 pit example analysis", after the study of this article, I believe that everyone has a deeper understanding of the Shell exit 0 pit example analysis, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report