Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic exit from Bash shell script on error [duplicate]

I've been writing some shell script and I would find it useful if there was the ability to halt the execution of said shell script if any of the commands failed. See below for an example:

#!/bin/bash  cd some_dir  ./configure --some-flags  make  make install 

So in this case, if the script can't change to the indicated directory, then it would certainly not want to do a ./configure afterwards if it fails.

Now I'm well aware that I could have an if check for each command (which I think is a hopeless solution), but is there a global setting to make the script exit if one of the commands fails?

like image 909
radman Avatar asked May 20 '10 04:05

radman


People also ask

How do I exit bash script error?

Exit When Any Command Fails This can actually be done with a single line using the set builtin command with the -e option. Putting this at the top of a bash script will cause the script to exit if any commands return a non-zero exit code.

How do you exit a script in shell script?

To end a shell script and set its exit status, use the exit command. Give exit the exit status that your script should have. If it has no explicit status, it will exit with the status of the last command run.

What is Pipefail in bash?

man bash says. pipefail. If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands in the pipeline exit successfully. This option is disabled by default.

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.


2 Answers

Use the set -e builtin:

#!/bin/bash set -e # Any subsequent(*) commands which fail will cause the shell script to exit immediately 

Alternatively, you can pass -e on the command line:

bash -e my_script.sh 

You can also disable this behavior with set +e.

You may also want to employ all or some of the the -e -u -x and -o pipefail options like so:

set -euxo pipefail 

-e exits on error, -u errors on undefined variables, and -o (for option) pipefail exits on command pipe failures. Some gotchas and workarounds are documented well here.

(*) Note:

The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test following the if or elif reserved words, part of any command executed in a && or || list except the command following the final && or ||, any command in a pipeline but the last, or if the command's return value is being inverted with !

(from man bash)

like image 72
Adam Rosenfield Avatar answered Oct 02 '22 20:10

Adam Rosenfield


To exit the script as soon as one of the commands failed, add this at the beginning:

set -e 

This causes the script to exit immediately when some command that is not part of some test (like in a if [ ... ] condition or a && construct) exits with a non-zero exit code.

like image 20
sth Avatar answered Oct 02 '22 18:10

sth