[u-u] wow, post-tested loops in sh
D. Hugh Redelmeier
hugh at mimosa.com
Mon Jul 4 10:25:24 EDT 2016
| From: Alan J Rosenthal <flaps at 56789.ca>
| All this follows from the syntax of the 'while' statement, but still, wow.
There's another lesson to take from this. The understanding of the shell
that you had formed (and almost everyone else has too) is different from
the real thing. That suggests that the real thing is wrong -- badly
designed for humans.
Using this feature would impose maintenance costs: every subsequent reader
of the code is likely to either misunderstand the code or be held up
trying to understand it.
The shell's generality in this case means it cannot quickly and clearly
diagnose cases where someone inadvertently uses this feature. This isn't
just theoretical: I've spent time trying to figure out where I went
wrong in a shell script when the diagnostic appears much later (end of
file?) than the mistake.
(To make bash less forgiving of mistakes, I always put "set -eu" early
in my scripts. Forgiveness is not always a blessing.)
| (In sh, control constructs have as their body "one or more statements"
| instead of the more modern "zero or more statements".)
On the other hand, this lack of generality is surprising. It still
catches me once in a while.
Bourne was an Algol 68 guy. It is not surprising that he'd
incorporate useless generality in the shell. The lack thereof is
surprising. Perhaps eliminating the empty statement list avoided an
ambiguity in the formal grammar.
The do statement in Algol 68 is a thing of wonder. In fact, all of
Algol 68 is a thing of wonder. Sometimes in a good way, sometimes not.
More information about the u-u