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

Re: /etc/shells management



On Sun, Sep 07, 2003 at 06:59:18PM +0100, Colin Watson wrote:
> On Sun, Sep 07, 2003 at 11:58:03AM -0500, Steve Langasek wrote:
> > On Sun, Sep 07, 2003 at 06:08:06PM +0200, Thomas Hood wrote:
> > > On Sun, 2003-09-07 at 17:23, Branden Robinson wrote:
> > > > Man, that's ugly.  I use:
> > > > 
> > > > if [ -n "$var" ]; then
> > > > fi
> > 
> > > I have been using
> > > 
> > >     if [ "$var" ]; then
> > >     fi
> > 
> > > I hope that's kosher too; otherwise I have a few scripts to fix.
> > 
> > In general, no.  If the contents of $var are a test operator, you'll get
> > a syntax error.
> 
> POSIX requires this not to be the case, because of the argument-counting
> algorithm that 'test' is supposed to follow. See
> http://www.opengroup.org/onlinepubs/007904975/utilities/test.html.

But if $var contains more than one shell word...

You might get different results dependening on whether you remember to
quote the shell variable or not.  My own preliminary tests leave this
unclear.  :)  Probably not worth leaving to the vagaries of shell
implementations, anyway.

It's just too unpredictable to rely upon.  Especially if $var can be a
variable that is derived from user input without Stalinistic validation.

I personally stongly advocate -n and -z over [ "$var" ] and really
horrible tests like:

if [ "x$var" = "x" ]
and
if [ "x$var" != "x" ]

Those just strike me as obscurantist (which might explain why they
feature prominently in shell scripts by Ian Jackson, heh).  test -n and
-z exist for a reason, even if one has to come up with pretty dodgy
mnemonics for remembering them.  The other benefit to using test flags
instead of a hand-rolled string test is that they're *documented in the
manpage*.

-- 
G. Branden Robinson                |    It may be difficult to to determine
Debian GNU/Linux                   |    where religious beliefs end and
branden@debian.org                 |    mental illness begins.
http://people.debian.org/~branden/ |    -- Elaine Cassel

Attachment: pgpEvmE_sSAY2.pgp
Description: PGP signature


Reply to: