What does set -e mean in a bash script?

匿名 (未验证) 提交于 2019-12-03 02:11:02

问题:

I'm studying the content of this preinst file that the script executes before that package is unpacked from its Debian archive (.deb) file.

The script has the following code:

#!/bin/bash set -e # Automatically added by dh_installinit if [ "$1" = install ]; then    if [ -d /usr/share/MyApplicationName ]; then      echo "MyApplicationName is just installed"      return 1    fi    rm -Rf $HOME/.config/nautilus-actions/nautilus-actions.conf    rm -Rf $HOME/.local/share/file-manager/actions/* fi # End automatically added section 

My first query is about the line:

set -e 

I think that the rest of the script is pretty simple: It checks whether the Debian/Ubuntu package manager is executing an install operation. If it is, it checks whether my application has just been installed on the system. If it has, the script prints the message "MyApplicationName is just installed" and ends (return 1 mean that ends with an “error”, doesn’t it?).

If the user is asking the Debian/Ubuntu package system to install my package, the script also deletes two directories.

Is this right or am I missing something?

回答1:

From help set :

  -e  Exit immediately if a command exits with a non-zero status. 

But it's not very reliable and considered as a bad practice, better use :

trap 'do_something' ERR 

to run do_something function when errors will occurs.

See http://mywiki.wooledge.org/BashFAQ/105



回答2:

set -e stops the execution of a script if a command or pipeline has an error - which is the opposite of the default shell behaviour, which is to ignore errors in scripts. Type help set in a terminal to see the documentation for this built-in command.



回答3:

As per bash - The Set Builtin manual, if -e/errexit is set, shell exits immediately if a pipeline consist of a single simple command, a list or a compound command returns a non-zero status.

By default, the exit status of a pipeline is the exit status of the last command in the pipeline, unless the pipefail option is enabled (it's disabled by default).

If so, the pipeline’s return status of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully.

If you'd like to execute something on exit, try defining trap, for example:

trap onexit EXIT 

where onexit is your function to do something on exit, like below which is printing the simple stack trace:

onexit(){ while caller $((n++)); do :; done; } 

There is similar option -E/errtrace which would trap on ERR instead, e.g.:

trap onerr ERR 

Examples

Zero status example:

$ true; echo $? 0 

Non-zero status example:

$ false; echo $? 1 

Negating status examples:

$ ! false; echo $? 0 $ false || true; echo $? 0 

Test with pipefail being disabled:

$ bash -c 'set +o pipefail -e; true | true | true; echo success'; echo $? success 0 $ bash -c 'set +o pipefail -e; false | false | true; echo success'; echo $? success 0 $ bash -c 'set +o pipefail -e; true | true | false; echo success'; echo $? 1 

Test with pipefail being enabled:

$ bash -c 'set -o pipefail -e; true | false | true; echo success'; echo $? 1 


回答4:

I found this question while Googling, trying to figure out what the exit status was for a script that was aborted due to a set -e. The answer didn't appear obvious to me; hence this answer. Basically, set -e aborts the execution of a command (e.g. a shell script) and returns the exit status code of the command that failed (i.e. the inner script, not the outer script).

For example, suppose I have the a shell script outer-test.sh:

#!/bin/sh set -e ./inner-test.sh exit 62; 

The code for inner-test.sh is:

#!/bin/sh exit 26; 

When I run outer-script.sh from the command line my outer script terminates with the exit code of the inner script:

$ ./outer-test.sh $ echo $? 26 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!