tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: sh weirdness?



From: der Mouse <mouse%Rodents-Montreal.ORG@localhost>
Subject: sh weirdness?
Date: Fri, 17 Sep 2010 15:52:36 -0400 (EDT)

> I've been trying to use some external software across various of my
> NetBSD machines, and have run into a problem that I've tracked down to
> a difference in sh behaviour.  Fortunately, a small test case exists.
> 
> Running this
> 
> sh -n -c 'case foo in (bar | baz) echo one; esac'
> 
> on 4.0.1 returns nothing, but on 1.4T, it gives
> 
> Syntax error: word unexpected (expecting ")")
> 
> Can anyone explain what this syntax (with the leading open paren and no
> closing ;;) does?  The 4.0.1 sh manpage is no help; it lists only the
> no-( with-;; syntax, even though the sh it supposedly describes accepts
> this mutant version.  Based on the context in which I found it, it
> looks as though the case above is probably semantically equivalent to
> "case foo in bar|baz) echo one;; esac", but I hesitate to just replace
> it without actually understanding what the code I'm replacing does.

It is equivalent indeed. As far as I know, this syntax is mandated by
POSIX Shell Command Language (e.g. as described in [1]). The leading
parenthesis as well as the ';;' for the last case are both optional.

 [1] http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html

Cheers,
Nhat

> 
> /~\ The ASCII                           Mouse
> \ / Ribbon Campaign
>  X  Against HTML              mouse%rodents-montreal.org@localhost
> / \ Email!         7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B


Home | Main Index | Thread Index | Old Index