The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
* [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

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

[-- Attachment #1: Type: text/plain, Size: 966 bytes --]

There seems to have been a migration over time from the first format to the
second, perhaps a result of C programmers not having a keyword "then",
which Bourne shells (following Algol 68) require.  I don't think it matters
much.

On Tue, Sep 29, 2020 at 2:52 PM David Barto <david@kdbarto.org> wrote:

> 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

[-- Attachment #2: Type: text/html, Size: 1350 bytes --]

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

* [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

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 18:42 [TUHS] SH script formatting David Barto
2020-09-29 19:29 ` John Cowan
2020-09-29 19:15 Norman Wilson

The Unix Heritage Society mailing list

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/tuhs

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 tuhs tuhs/ http://inbox.vuxu.org/tuhs \
		tuhs@minnie.tuhs.org
	public-inbox-index tuhs

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.tuhs


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git