The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
From: George Michaelson <ggm@algebras.org>
To: TUHS main list <tuhs@minnie.tuhs.org>
Subject: Re: [TUHS] sh and goto
Date: Wed, 16 Dec 2020 09:27:28 +1000	[thread overview]
Message-ID: <CAKr6gn1S-oHou3MP_gmAzpAQL_+19PTV-BE1V1k3h_BHAixkMw@mail.gmail.com> (raw)
In-Reply-To: <CAMP=X_mrdar5j3v0_wuDTEQdSyyVsNdOv4_fcL8BtGdRCHP_eg@mail.gmail.com>

HN has a story about a safe bash script template. Putting to one side
the oxymoronic qualities of the sentence, the thing which stands out
is the use of trap to a function on SIG<x> -Which we all need, but
often don't do. Thumping ^C repeatedly when things go unexpectedly
wrong, is a fact of life. What do you want your script to do?

Coding for the unexpected is just hard. Going into a script (or
program) pre-thinking that you have to 'expect the unexpected' breaks
the design imperative of planning for success. IF-THEN-ELSE doesn't
always cope well with mental models of "do A,B,C but if Z happens get
2 dozen milk" (which leads to the joke about programmers shopping
instructions with an IF clause in it)

The trap thing in shell always confused me. What state am I "in" when
I wind up in that trap? do variables exist (no: IIRC the var=value
flow in a shellscript is lazy so the un-instantiated x=y inside an if
expression you don't know was reached or not MAY not exist)  has
sub-job completed, (can trap pre-empt a wait statement? of course it
can! what does it mean for children reaping... you do know the PID
right?)

None of this is rocket surgery. It goes to lawyer-like legalistic "one
side of one cow in one field is black" reasoning -which for a program
is probably not a bad thing, but it isn't very human(e) thinking for
some people.

Then enters ^T (the TOPS-10 inherited "what state am I in, in terms of
CPU time and I/O") which somebody *else* is dealing with: the thing
which called your shell instance..

trap is a setjmp/longjmp type outcome. You pop up in a call you didn't
exactly "plan" for (well you did: you wrote it. But you don't know the
entry state which led you there, unlike normal program flow) -well, it
is.. because you get there. But you aren't going back to your normal
logic flow anytime soon in any shellscript I saw.

  reply	other threads:[~2020-12-15 23:28 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-15 16:02 srbourne
2020-12-15 19:44 ` Ken Thompson
2020-12-15 23:27   ` George Michaelson [this message]
2020-12-16  4:30     ` Richard Salz
2020-12-16  4:41       ` George Michaelson
2020-12-16  4:01 M Douglas McIlroy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAKr6gn1S-oHou3MP_gmAzpAQL_+19PTV-BE1V1k3h_BHAixkMw@mail.gmail.com \
    --to=ggm@algebras.org \
    --cc=tuhs@minnie.tuhs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).