Automatic exit from bash shell script on error

2019-01-03 00:40发布

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?

标签: bash shell exit
7条回答
Anthone
2楼-- · 2019-01-03 01:04

One idiom is:

cd some_dir && ./configure --some-flags && make && make install

I realize that can get long, but for larger scripts you could break it into logical functions.

查看更多
迷人小祖宗
3楼-- · 2019-01-03 01:05

Here is how to do it:

#!/bin/sh

abort()
{
    echo >&2 '
***************
*** ABORTED ***
***************
'
    echo "An error occurred. Exiting..." >&2
    exit 1
}

trap 'abort' 0

set -e

# Add your script below....
# If an error occurs, the abort() function will be called.
#----------------------------------------------------------
# ===> Your script goes here
# Done!
trap : 0

echo >&2 '
************
*** DONE *** 
************
'
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-03 01:12

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.

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-03 01:13

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.

(*) 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)

查看更多
Luminary・发光体
6楼-- · 2019-01-03 01:13

I think that what you are looking for is the trap command:

trap command signal [signal ...]

For more information, see this page.

Another option is to use the set -e command at the top of your script - it will make the script exit if any program / command returns a non true value.

查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-01-03 01:15

Use it in conjunction with pipefail.

set -e
set -o pipefail

-e (errexit): Abort script at first error, when a command exits with non-zero status (except in until or while loops, if-tests, list constructs)

-o pipefail: Causes a pipeline to return the exit status of the last command in the pipe that returned a non-zero return value.

Chapter 33. Options

查看更多
登录 后发表回答