[Top][All Lists]

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

Re: [PATCH] add proper variable quoting to grub-mkconfig_lib (Debian bug

From: Colin Watson
Subject: Re: [PATCH] add proper variable quoting to grub-mkconfig_lib (Debian bug #612417)
Date: Tue, 5 Apr 2011 21:53:06 +0100
User-agent: Mutt/1.5.20 (2009-06-14)

On Tue, Apr 05, 2011 at 10:07:46PM +0200, Nicolas de Pesloüan wrote:
> On the same kind of things, the construct if test "x$foo" = "x" is
> pointless. It is a very strange heritage from DOS, where IF x%A = x
> was a common construct, because DOS ( lack quoting. (Is
> wasn't possible to write IF "%a" = ""). In shell, "" is an empty
> string, but a real argument to commands. The following construct is
> the good one : if test "$foo" = "". The test command will receive
> three arguments: the value of $foo, the = sign and an empty argument
> and will return true if $foo happens to be empty.

Actually, it's more relevantly a heritage from pre-POSIX shells, which
became habit for many GNU programmers via Autoconf.  Here's what the
Autoconf documentation has to say on the subject:

`test' (strings)
     Posix says that `test "STRING"' succeeds if STRING is not null,
     but this usage is not portable to traditional platforms like
     Solaris 10 `/bin/sh', which mishandle strings like `!' and `-n'.

     Posix also says that `test ! "STRING"', `test -n "STRING"' and
     `test -z "STRING"' work with any string, but many shells (such as
     Solaris, AIX 3.2, UNICOS, Digital Unix 4, etc.) get
     confused if STRING looks like an operator:

          $ test -n =
          test: argument expected
          $ test ! -n
          test: argument expected

     Similarly, Posix says that both `test "STRING1" = "STRING2"' and
     `test "STRING1" != "STRING2"' work for any pairs of strings, but
     in practice this is not true for troublesome strings that look
     like operators or parentheses, or that begin with `-'.

     It is best to protect such strings with a leading `X', e.g., `test
     "XSTRING" != X' rather than `test -n "STRING"' or `test !

Solaris 10 is not that old, and this has long been a problem for users
of /bin/sh on Solaris in particular.  Note that GRUB works on Solaris.
I would be interested to know if OpenSolaris, or even Illumos, still
uses a pre-POSIX /bin/sh.

In most cases, of course, this doesn't matter since it only affects
certain strings.  But on packages that run on Solaris I am always
extremely reticent to change existing code to assume POSIX shell, even
if I might write new code that way.

When I can assume POSIX shell, your version:

  if test "$foo" = ""

is needlessly verbose.  The standard stipulates that this works just

  if test "$foo"

> Writing solid code imply - in particular - knowing the exact behaviors
> of the programming language :-)

On Unix-like systems, though, and particularly when it comes to shell,
this often involves knowing about historical behaviour as well as
current behaviour.

Colin Watson                                       address@hidden

reply via email to

[Prev in Thread] Current Thread [Next in Thread]