The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
* [TUHS] SH script formatting
@ 2020-09-29 19:15 Norman Wilson
  0 siblings, 0 replies; 3+ messages in thread
From: Norman Wilson @ 2020-09-29 19:15 UTC (permalink / raw)
  To: tuhs

if test; then
	stuff

and

if test
then
	stuff

are functionally equivalent.  I wouldn't say one or the
other `is preferred.'  I use the former because I think
it's a little more readable because more compact.  But
it's really a matter of style, like whether you write

	if (test) {
		(multi-statement block)

or

	if (test)
	{
		(multi-statement block)

I have a stronger opinion about those who use overly-
cryptic constructions like

	test && {
		shell commands
	}

because it means exactly the same thing as

	if test; then
		shell commands

but is more obscure to read.  But again it's a question
of style, not of dogma.

As an aside, I think one excuse that is sometimes used
for that sort of construct is when it's

	test || {
		commands
	}

because Bourne's original shell had no not operator.
For a long time after shell functions appeared, I would
add this function to any of my shell scripts that needed
it:

	not() {
		if "$@"; then
			return 1
		else
			return 0
	}

so I could say

	if not test; then
		commands
	fi

Modern Bourne-shell descendants have a built-in ! operator:

	if ! test; then
		commands
	fi

I'm not keen on most of what has been stuffed into bash and
ksh and the like, but ! is a real improvement.  I believe
POSIX mandates it, and I think they're right.

Norman Wilson
Toronto ON

^ permalink raw reply	[flat|nested] 3+ messages in thread
* [TUHS] SH script formatting
@ 2020-09-29 18:42 David Barto
  2020-09-29 19:29 ` John Cowan
  0 siblings, 1 reply; 3+ messages in thread
From: David Barto @ 2020-09-29 18:42 UTC (permalink / raw)
  To: TUHS main list

In a brief discussion with a coworker today the question of formatting shell scripts came up.

I believed that in the past the preferred format (if there ever were any such thing) was

if [ test ]
then
	statements
else
	statements
fi

I can find nothing specific to back this up. More appropriate for COFF maybe would
be a discussion of what format is better

if [ test ]; then
	statements
else
	statements
fi

or the above.

No intention to start any kind of flame war about which is better, just want to see
if there is any historical option for one over the other.

	David

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-09-29 19:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-29 19:15 [TUHS] SH script formatting Norman Wilson
  -- strict thread matches above, loose matches on Subject: below --
2020-09-29 18:42 David Barto
2020-09-29 19:29 ` John Cowan

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