In our last episode, responding directly to Stan, Greg wrote
(among other things):
>set -e is not designed to be user-friendly or to conform to your
>expectations of how a well-designed error handler should behave.
>set -e is designed to conform to POSIX's ever-changing wordings and
>interpretations as they attempt to pin down the correct language to
>describe the varying implementations of this LEGACY feature.
>...
>As I will continue to point out until I die, apparently, because nobody
>ever seems to believe this until they've heard it a hundred times,
>set -e is not your friend. You should not be using it in new scripts.
>It's only there because POSIX requires it, to support legacy scripts
>that were written to use it (and to support Makefiles).
>set -e is not an automatic error handling mechanism.
First, a few points of order:
1) You (Greg) need to more clearly distinguish between your use of
"personal you" vs. "rhetorical you". Most of what you wrote seems
more directed at the general public than to me personally. I
think we mostly agree as to how/when "set -e" can be used.
Point: There are multiple interpretations of what the word "use"
actually means. It could mean "use" as in "use at all" - i.e., if
you have "set -e" in your script, you are immediately condemned to
purgatory - or it could mean "use" as in "Use to the exclusion of
all else and depend on it entirely". I think many readers assume
you mean the former when in fact you mean mostly the later.
2) This post would probably be unnecessary (and I wouldn't have
bothered) except for the fact that you (Greg) have status here and
people (e.g., Chris) tend to follow your lead and make
recommendations to newbies accordingly. So, I feel a response is
warranted.
3) As I've mentioned many times, "trap ... ERR" is the
modern/current version of "set -e". I tend to use the trap form
in most new code, but some older scripts still use the set form.
Anything I say below about "set -e" applies equally to the trap
form.
>set -e is not a reasonable substitute for proper error handling.
Totally agree. It is not a substitute, but rather a fallback.
As I've argued in the past, it is a good "last fallback". That is,
you set -e, but you still do all the usual error handling for the
expected cases. The point here is that literally *anything* you do,
could error out. For example, a perfectly normal:
echo "This is a message" > afile
could error for a number of reasons. You might run out of disk space.
You might be in a write-protected directory. Even if you use
/tmp/afile, some wayward admin might have made /tmp non-writable. The
disk might have a head crash. The drive might have gone read-only
(say, it was an SD card). Etc.
All of these things are extremely unlikely, of course, but if any of
them does happen, you want your script to exit immediately. And you
really don't want to have to write explicit error-handling code for
every single trivial line such as the above example.
This is the way that I primarily use "set -e" - to catch unlikely
events that I cannot reasonably predict/program for, but which I do
want to catch (and exit from).
=================================================================================
Please do not send me replies to my posts on the list.
I always read the replies via the web archive, so CC'ing to me is unnecessary.
When responding to my posts, please try to refrain from giving bureaucratic
answers.
If you have nothing useful to say, then just click Next and go on.