Skip to contentSkip to conversation

if then elif then else fi -- Problem report

DISCUSSION Malcolm Kay 3 participants 7 messages Jun 28, 2010 – Jul 6, 2010

lore
Share & tools
lore
#0 lore raw

I notice that 'dash' fails on many scripts that ran okay with the traditional Bourne shell 'sh'.

On closer examination I find that it does not conform to any of the man pages that I've been able to find for 'dash'.

Should I create a problem report? Where should I send it?

The particular version is dash-0.5.5.1 running on FreeBSD xi.home 8.0-RELEASE FreeBSD 8.0-RELEASE #0:
Sat Nov 21 15:48:17 UTC 2009

I have a report that the same trouble occurs in the 'dash' as shipped with Ubuntu 10.04 Lucid Lynx.

The problem is that 'dash' bombs out with an empty list in:

if ...
then
list
elif ...
then
list
fi

or in:

if ...
then
list
else
list
fi

The latter gives for example:
Syntax error: "else" unexpected.

Yet the man pages clearly state that a list consists of zero or more commands --
<quote>
Lists -- Generally Speaking
A list is a sequence of zero or more commands separated by
newlines,
semicolons, or ampersands, and optionally terminated by one
of these
three characters.
</quote>

Should I create a problem report? Where should I send it?

Cheers,

Malcolm

#1 lore raw
Quoted 1 line · Malcolm Kay

Should I create a problem report? Where should I send it?

You just did.

Quoted 10 lines

The problem is that 'dash' bombs out with an empty list in:

if ...
then
list
elif ...
then
list
fi

Quoted 8 lines

or in:

if ...
then
list
else
list
fi

POSIX requires a non-empty list in these syntax setups.

Quoted 2 lines

The latter gives for example:
Syntax error: "else" unexpected.

Correct.

Quoted 6 lines

Yet the man pages clearly state that a list consists of zero or more commands --
<quote>
Lists -- Generally Speaking
A list is a sequence of zero or more commands separated by
newlines,

Therefore, the bug is in the man page. Per POSIX,
http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_03
"A list is a sequence of one or more AND-OR lists separated by the operators ';' and '&' and optionally terminated by ';' , '&' , or <newline>."

Signature · 2 lines
Eric Blake   [email protected]    +1-801-349-2682
Libvirt virtualization library http://libvirt.org
#2 lore raw

Eric,
Thanks for the clarification.

I had assumed that 'dash' aimed to be a faster replacement for the classical Bourne shell 'sh' as implemented in BSD systems, and this seemed to be confirmed when all the man pages I've been able to find were more or less direct copies of BSD 'sh' pages.

As it is I feel the man pages should not only be fixed in this respect, but also such differences to classical 'sh' implementations should be high lighted.

Quoted 7 lines · Eric Blake

. . .

Therefore, the bug is in the man page. Per POSIX,
http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_ch
ap02.html#tag_18_09_03 "A list is a sequence of one or more AND-OR lists separated by the operators ';' and '&' and optionally terminated by ';' , '&' , or <newline>."

While I agree with your conclusions, I would note that POSIX defines "if ... else etc." in terms of 'compound_list' rather than 'list' so the above definition of 'list' is not really relevant. By my reading of your reference it seems POSIX defines 'list' and 'compound_list' independently but fails to clarify the difference if any.

Malcolm

#3 lore raw
Quoted 5 lines · Malcolm Kay

Eric,
Thanks for the clarification.

I had assumed that 'dash' aimed to be a faster replacement for the classical Bourne shell 'sh' as implemented in BSD systems,

Rather, 'dash' aims to be the fastest and smallest possible POSIX-compliant shell; anywhere that POSIX disagrees with traditional Bourne shell behavior, POSIX triumphs. Not all BSD system /bin/sh are POSIX compliant. And while dash has some extensions over POSIX, the goal of being smallest means that extensions are kept to a minimum (contrast that with bash or zsh, which both have a goal of providing as many useful extensions as possible at the expense of size and sometimes speed).

Quoted 3 lines

As it is I feel the man pages should not only be fixed in this respect, but also such differences to classical 'sh' implementations should be high lighted.

There are other existing online resources that describe differences between Bourne shell and POSIX. But it is a much bigger effort to document how dash differs from non-POSIX shells than it is to just document how dash itself behaves.

Quoted 10 lines · Eric Blake
Quoted 7 lines

. . .

Therefore, the bug is in the man page. Per POSIX,
http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_ch
ap02.html#tag_18_09_03 "A list is a sequence of one or more AND-OR lists separated by the operators ';' and '&' and optionally terminated by ';' , '&' , or <newline>."

Quoted 6 lines

While I agree with your conclusions, I would note that POSIX defines "if ... else etc." in terms of 'compound_list' rather than 'list' so the above definition of 'list' is not really relevant. By my reading of your reference it seems POSIX defines 'list' and 'compound_list' independently but fails to clarify the difference if any.

POSIX defines compound_list quite clearly. Read the grammar section:
http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_10_02

compound_list    :              term
                 | newline_list term
                 |              term separator
                 | newline_list term separator

It also defines list:

list : list separator_op and_or
| and_or

The difference between the two is how newlines serve to separate the various and_or lists.

Signature · 2 lines
Eric Blake   [email protected]    +1-801-349-2682
Libvirt virtualization library http://libvirt.org
#4 lore raw
In reply to Malcolm Kay
Quoted 4 lines · Malcolm Kay

I had assumed that 'dash' aimed to be a faster replacement for the classical Bourne shell 'sh' as implemented in BSD systems, and this seemed to be confirmed when all the man pages I've been able to find were more or less direct copies of BSD 'sh' pages.

The original ash (at the time of 4.3BSD-Net2) was such a replacement of the SVR4 Bourne shell. But with the next "traditional" BSD release, 4.4BSD alpha, the shell was already aiming at POSIX; and so were the shells on NetBSD (1.0 ff) and FreeBSD, soon.

However, there is a relation from 4.3BSD-Net2, 386BSD, earlyNetBSD to a Linux port, and thus a Bourne-like ash interestingly survived with Slackware until 2007 (8.1).

dash in contrast was an independent, later port from NetBSD to Linux and was a modern variant from the start.

#5 lore raw
Quoted 7 lines · Sven Mascheck
Quoted 5 lines · Malcolm Kay

I had assumed that 'dash' aimed to be a faster replacement for the classical Bourne shell 'sh' as implemented in BSD systems, and this seemed to be confirmed when all the man pages I've been able to find were more or less direct copies of BSD 'sh' pages.

Quoted 5 lines

The original ash (at the time of 4.3BSD-Net2) was such a replacement of the SVR4 Bourne shell. But with the next "traditional" BSD release, 4.4BSD alpha, the shell was already aiming at POSIX; and so were the shells on NetBSD (1.0 ff) and FreeBSD, soon.

I recall seeing and even playing with an implementation of the Almquist shell 'ash' in the mid 1980's but as it was running on an unimpressive/unsuitable operating system (DRDOS/TOS) it was not really very inspiring.

Later I picked up a microVAX with Ultrix 4, I still have the manuals. The man pages reference sh(1) as "the standard Bourne shell interpreter" and sh5(1) as a "version of the shell from System V version 2". In general the sh(1) pages has seemed to conform fairly closely to 'sh' as found on modern BSD operating systems. But now that I look more closely I find that both the Ultrix man pages define a list as "one or more ..." , rather the "zero or more ..." as in later man pages -- so it would seem at least in that respect the Ultrix corresponds more closely to what was then a not yet existing POSIX standard.

In a way I do find this rather surprising as POSIX appeared in an era when languages such as fortran and basic which started counting at one were becoming displaced (admittedly rather slowly) by languages that incorporated zero, C, pascal and virtually all the modern OOP languages.

Quoted 7 lines

However, there is a relation from 4.3BSD-Net2, 386BSD, earlyNetBSD to a Linux port, and thus a Bourne-like ash interestingly survived with Slackware until 2007 (8.1).

dash in contrast was an independent, later port from NetBSD to Linux and was a modern variant from the start.

History can be interesting and sometimes somewhat surpising.

Malcolm

#6 lore raw
Quoted 3 lines · Malcolm Kay

I recall seeing and even playing with an implementation of the Almquist shell 'ash' in the mid 1980's but as it was running on an unimpressive/unsuitable operating system (DRDOS/TOS)

That must have been a different shell, because the Almquist shell was introduced '89.

Quoted 4 lines

Later I picked up a microVAX with Ultrix 4, I still have the manuals. The man pages reference sh(1) as "the standard Bourne shell interpreter" and sh5(1) as a "version of the shell from System V version 2".

Ultrix sh is a variant of the 7th edition shell, the original Bourne shell. (Ultrix eventually got somewhat famous for coming with such an old variant as /bin/sh, because it had not even implemented functions, yet). Ultrix sh5 is a later variant of the same family, which was mainly added because it had functions. If you want to have a glance at such relationsships from the
past: http://www.in-ulm.de/~mascheck/various/