Föderation EN Di 27.08.2024 06:11:20 #!/bin/sh set -euo pipefail thanks, @b0rk - I've been writing shell for 25+ years and wish I'd been doing this 25+ years ago. (Medien: 1) |
Föderation EN Di 27.08.2024 09:56:26 (Medien: 1) |
Föderation EN Di 27.08.2024 13:33:11 @gremlins @darkuncle @b0rk orrr, if you are using an actual posix shell… (expression) || (echo 'error: thingy happened wrong'; break) will solve most of these 'issues'. |
Föderation EN Di 27.08.2024 15:06:44 @wyatt8740 |
Föderation EN Di 27.08.2024 10:30:35 @darkuncle @b0rk habitually setting pipefail will drive you crazy next time you use grep -q. |
Föderation EN Di 27.08.2024 10:56:55 @darkuncle @b0rk Hrm. So, 1) checking $? is shell-agnostic and generally more appropriate, 2) never rm recursively if you can help it, and check your variables prior to throwing them into something potentially destructive, and 3) how did that grep succeed? In general, shell programming isn't any different from anything else - beware of assumptions.
|
Föderation EN Di 27.08.2024 11:41:38 @darkuncle @b0rk beware: `-o pipefail` is not defined in POSIX spec, so `/bin/sh` may not recognize it (`/bin/bash` would) See ShellCheck Wiki for a POSIXly correct way of achieving a similar effect: https://www.shellcheck.net/wiki/SC3040 |
Föderation EN Di 27.08.2024 12:56:42 One I find really helpful (but missing here) is 'set -x', that prints every line during execution. |
Föderation EN Di 27.08.2024 14:54:37 @panda @darkuncle @b0rk she's got it covered in the full zine, and here's the comic for it |
Föderation EN Di 27.08.2024 13:04:32 @darkuncle @b0rk |
Föderation EN Di 27.08.2024 14:06:30 @darkuncle isn’t it great? I also enabled shellcheck/shfmt in my editor and it was transformative enough that I no longer think “this is a mistake” every time I start a non-trivial script (my bar for that is now non-scalar variables). |
Föderation EN Di 27.08.2024 14:14:50 @darkuncle @b0rk and now you're doing it in the wrong shell, by making a single assumption. |
Föderation EN Di 27.08.2024 14:39:16 @darkuncle Btw, there's perhaps an error, perhaps an oversimplification in the comic: the 'fail the whole pipeline' logic is, by default, the exit status of a pipeline command comes from the exit status of its final component. It's well documented and an old idea, although, in retrospect, perhaps not the best idea. Unix is full of such. |
Föderation EN Di 27.08.2024 14:49:34 @darkuncle @b0rk I love this, but reading your replies reminded me why I am a computer science recluse. 😂 |