|
|
|
|
|
by xyzzy_plugh
1863 days ago
|
|
> The rules are too tricky to remember even for shell experts -- there are persistent arguments on POSIX behavior that is over 20 years old, simply because it's so confusing. I don't know, I find it easier to not use set -e. I find it significantly easier to just explicitly handle all my errors. Having my script exit at some arbitrary point is almost never desirable. I find chaining && and || pretty intuitive. var=$(myfunc) &&
echo OK ||
{
echo Not OK
exit 1
}
This is pretty contrived. I'd probably put the error handling in a function and then only handle the failure scenario: var=$(myfunc) || die 'Not OK'
echo OK
I never run into problems, this always works as expected, I don't need any language or interpreter changes to fix it. Once you realize `if` is just syntactic sugar and [ is just `test` then the world gets pretty simple. |
|
Are you regularly writing shell scripts that check all their errors? I'd be curious to take a look if any of them are public.
It's not impossible to do this -- git's shell scripts seem to do a decent job. However I think the vast majority of shell scripts don't check errors, so "set -e" is "closer to right", if not right. (And I claim it's nearly impossible to be "right" with state of the art with -e -- fixes in the shell itself are needed.)
I'll also note that Alpine Linux's apk package manager switched to "set -e" a few years ago. They are shell experts and even they found it difficult to check all their errors without it. apk is more than 1000 lines of shell IIRC.