简体   繁体   中英

Automatic exit from bash shell script on error: set +e does not seem to do the job

While I knew for 20 years that shell scripts do not care about errors I keep being amused about this carelessness by default. And even when you explicitly require them not to swallow error and to follow crash early principle that still does not happen.

Referring to Automatic exit from bash shell script on error , set +e does not seem to do the job, here is a short example:

#!/bin/bash -vx
set +e
apt-get install nonexisting1
apt-get install nonexisting2
set -e

Output:

#!/bin/bash -vx
set +e
+ set +e
apt-get install nonexisting1
+ apt-get install nonexisting1
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package nonexisting1  <--- first error, now stop!
apt-get install nonexisting2           <--- why do we continue here?
+ apt-get install nonexisting2
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package nonexisting2
set -e
+ set -e

How do I make sure that my script either does all the commands without error or stops immediately? I do not like to write || exit 1 || exit 1 at the end of almost every line.

Options in shell are (counter-intuitively) switched on using a minus and switched off using a plus.

Even so, the set -e option will only work if the program correctly returns non-zero error statuses.

Although the apt-get manual says it should do this, other posts indicate that it frequently does not (see apt-get update exit status ).

Verify the return status of your apt command after it runs (eg using echo $? ). If it returns non-zero, set -e should work.

I guess you are simply using set +e in place of set -e and vice versa. If you simply do

#!/bin/bash -vx
set -e
apt-get install nonexisting1
apt-get install nonexisting2
set +e

it should stop after the first line.

Why you set "+e" first of script ?
this is my example :

#!/bin/bash  -xv

set -e 

dskakj

echo "Hello dear" 
apt-get install bash

output :

#!/bin/bash  -xv

set -e 
+ set -e

dskakj
+ dskakj
./a.sh: line 5: dskakj: command not found

Note : not need set "+e" end of script .

For a four-line script like yours, set -e does feel reasonable, assuming apt-get does exit with a non-zero exit code for errors.

However, lots of discussions regarding set -e on SO indicate that it is not a reliable technique for error handling. The Bash documentation confirms it.

We have no option but to check the status variable $? after each command if we want our script to be reliable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM