hhmx.de

Scott Francis

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)

Wyatt (🏳️‍⚧️♀?)

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'.
except unset variables. You should always set any variables you plan to use or check for their existance at the start of a script anyway, especially if you ever used csh you know why.

Oriel Jutty

Föderation EN Di 27.08.2024 15:06:44

@wyatt8740 break in a subshell? Where does that work?

Bracken :verified:

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.

Mason Loring Bliss

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.

Nikita Karamov

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: shellcheck.net/wiki/SC3040

propapanda :verified:

Föderation EN Di 27.08.2024 12:56:42

@darkuncle

One I find really helpful (but missing here) is 'set -x', that prints every line during execution.

@b0rk

Richard McWizard

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 :blobwizard:
wizardzines.com/comics/bash-de

Wolf480pl

Föderation EN Di 27.08.2024 13:04:32

@darkuncle @b0rk
except `set -e` has bizzare edge cases that can make you script behave in an unexpected way
mywiki.wooledge.org/BashFAQ/10

Chris Adams

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).

Angus Marshall :2001: :linux:

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.

Riley S. Faelan

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.

@b0rk

Matthew Merkovich?

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. 😂