bash errors

wizard zines
@b0rk_reruns @b0rk grep won't succeed in that example unless curl's standard output matches that pattern, which is unlikely if curl failed. However, without pipefail or referencing ${PIPESTATUS[0]} for curl's exit code, you won't know if curl failed or if its lack of pandas caused grep to fail.

@b0rk_reruns
You can also use && to specify that you need both pieces to execute properly.

So you can do unzip fle.zip && cat file1 and if it can't unzip that file because it doesn't exist, then it won't run that cat command.

@b0rk_reruns @indigoparadox There are some added rules about conditional dispatch for set -e, but they themselves perserve the general hackiness of bash.
@b0rk_reruns
I use
set -o errexit -o nounset -o pipefail
Because it is easier to understand.
Also, it took me ages to remember which option is which and `set -eu` is not very intuitive.
@schnatterer @b0rk_reruns "Also, it took me ages to remember which option is which and `set -eu` is not very intuitive." - well, if one lives in EU πŸ‡ͺπŸ‡Ί it makes much more sense, I guess πŸ˜ƒ

@b0rk the 'unzip' in there reminds me of finding out last year that

zip output.zip foo bar baz

exits with a failure status if _none_ of foo, bar and baz exists – but if just one of them exists, it will print warnings for the rest, carry on, and exit 0 as if there had been no problem!

Turned out a file had been left out of one of my distribution zips for several releases and the warnings had been lost in huge build logs :-(

@simontatham
@b0rk

I recently experienced the opposite behavior with bash arithmetics: errexit failed a rather valid script https://floss.social/@schnatterer/111838296606152661
TLDR - arithmetic expressions in bash return (obviously) a numerical result. In combination with errexit return values not equal zero are interpreted as error 🀦

Johannes Schnatterer (@[email protected])

Attached: 2 images You're all using "-o errexit" in your #bash scripts to fail early, right? Did you know, that this does not go well with bash arithmetics? Let me explain: I recently reviewed this script: while [ -z "$some_value" ]; do ((retries++)) After enabling errexit it failed. Why? The arithmetic expression not only increments the counter but also returns the new value as exit code. This is != 0, which is interpreted as an error, leading to exit πŸ™ˆ

FLOSS.social
@schnatterer @simontatham @b0rk As the #freebsd Almquist sh(1) manual notes. "It is recommended to check for failures explicitly instead of relying on -e because it tends to behave in unexpected ways, particularly in larger scripts." https://man.freebsd.org/cgi/man.cgi?sh
sh

@b0rk_reruns This is a great overview that highlights one of my deep frustrations with older tools: Sometimes all the functionality is there but the defaults are wrong.

That's a concern (and sometimes, enough of a concern to justify a whole new tool) because you have to teach every new user the "right way" to hold the tool. It's as if nobody ever invented the hammer and we all just pounded nails with the back of an axe blade... Folks learn to not stab themselves in the face on the backswing, but sooner or later someone just invents the hammer and life is better.

@b0rk_reruns Oh wow. I needed this *today*. Thank you!
@b0rk_reruns While using set -euo pipefail will make bash a bit saner, real solution is to switch to real programming language. Python is often suitable choice.
@pavel @b0rk_reruns That’s not always a good or sane choice. Bash is highly portable as it exists on nearly all non-windows platforms by default. Not true of Python.
@b0rk_reruns what a good bot! #til Thank you, @b0rk
@b0rk_reruns @b0rk If you want to selectively guard against unset variables, you can do "${FOO?}"
@b0rk_reruns if you're using subshells, be sure to include `set -E`, as well. Otherwise something like `response=$(curl yxqzq.ca)` can fail silently, too.

@b0rk_reruns that's all very nice, but I'll still default to a languages with formal definition instead of a shrug and a wink on top of 60 years of conventions.

Unless I inevitably can't, of course.

@b0rk_reruns Would this have helped Intuitive Machines get the lander on the moon in a snap?

No?

Oh well, bash on regardless.

@b0rk_reruns

Ah yes, the Steam bug which would literally nuke your drive. All caused by an empty string.