One good way to deal with failures is to exit immediately, by adding this at the top:
However, this comes at a cost, commands that are "allowed to fail" now will kill your bash script.
set -o errexit
An elegant way to deal with this is to use parenthesis with "monad-ish" or blocks, which simply say "if this fails, then do this instead". In the error condition, you can echo a warning.
Nice and clean. The lsx above will fail, but the "ls" command that runs after it will succeed, causing the parenthesized statement to return 0, thus the script goes on executing, with the added benefit that i've clearly defined what to do when a command doesnt succeed.

No comments:
Post a Comment