I have a shell script that executes a number of commands. How do I make the shell script exit if any of the commands exit with a non-zero exit code?
相关问题
- How to get the return code of a shell script in lu
- JQ: Select when attribute value exists in a bash a
- Invoking Mirth Connect CLI with Powershell script
- Emacs shell: save commit message
- bash print whole line after splitting line with if
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- In IntelliJ IDEA, how can I create a key binding t
- Check if directory exists on remote machine with s
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
If you want to work with $?, you'll need to check it after each command, since $? is updated after each command exits. This means that if you execute a pipeline, you'll only get the exit code of the last process in the pipeline.
Another approach is to do this:
If you put this at the top of the shell script, it looks like bash will take care of this for you. As a previous poster noted, "set -e" will cause bash to exit with an error on any simple command. "set -o pipefail" will cause bash to exit with an error on any command in a pipeline as well.
See here or here for a little more discussion on this problem. Here is the bash manual section on the set builtin.
If you just call exit in the bash with no parameters, it will return the exit code of the last command. Combined with OR the bash should only invoke exit, if the previous command fails. But I haven't tested this.
The Bash will also store the exit code of the last command in the variable $?.