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
#!/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 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.
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 (e.g. using
echo $?). If it returns non-zero,
set -e should work.