[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: errexit is not suspended in a pipeline
From: |
Robert Elz |
Subject: |
Re: errexit is not suspended in a pipeline |
Date: |
Thu, 12 Jan 2023 14:06:32 +0700 |
What should be the real issue here is that the -e option is mostly a VERY
poor idea to ever use, unless you really know how it works, and only people
who are either shell implementors (and perhaps not even all of them) and
very long time shell users who have been bitten by attempts to use -e, tend
to be in that category.
Almost universally, the advice is "don't use -e". People keep wanting
to use it as some kind of shortcut to solve all their error handling
problems -- easy, just have the shell error out and stop whenever something
fails. Sounds great. But then all the times when it either works "too
well", or doesn't work when it was expected to, start to bite, and you
end up spending far more time fighting to try and make -e do what you
wanted it to do, than if you simply looked at every command, and for any
where failure should result in the script aborting, just add
|| fail "xyz produced status $?"
to those commands, and never use -e. That works exactly as coded, where
coded, and with messages that (if we make them) make sense to the user.
We, of course, also add (right up near the beginning):
fail() { printf >&2 '%s\n' "$*"; exit 1; }
unless that, or something similar (in which case just use it) already exists.
Use whatever exit status makes sense - even make it an arg to "fail" if
you like, though you don't get any option if you have sh abort because of -e.
kre