[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: What shells fail to work if comparing with "" ?
From: |
Stefano Lattarini |
Subject: |
Re: What shells fail to work if comparing with "" ? |
Date: |
Thu, 10 Feb 2011 20:55:25 +0100 |
User-agent: |
KMail/1.13.3 (Linux/2.6.30-2-686; KDE/4.4.4; i686; ; ) |
On Thursday 10 February 2011, Dr David wrote:
> I know its considered bad practice to check for an empty string with
> something like:
>
> if [ "$STR" = "" ] ; then
>
> but what shells do actually break with this, and under what conditions?
>
Solaris 10 /bin/sh breaks with [ "$var" != "" ] for some (very corner-case)
values of $var:
$ /bin/sh -c 'var="("; [ "$var" != "" ]'; echo st = $?
/bin/sh: test: argument expected
st = 1
$ /bin/sh -c 'var="!"; [ "$var" != "" ]'; echo st? = $?
/bin/sh: test: argument expected
st = 1
With [ "$var" = "" ], it doesn't really break, but misbehaves by printing
spurious diagnostic:
$ /bin/sh -c 'var="!"; [ "$var" = "" ]'; echo st = $?
/bin/sh: test: argument expected
st = 1
$ bin/sh -c 'var="("; [ "$var" != "" ]'; echo st = $?
/bin/sh: test: argument expected
st = 1
The exit status is correct in this cases, though.
> I was proposing someone change a test like that to
>
> if [ "x$STR" = ] ; then
>
I guess you mean:
if [ "x$STR" = x ] ; then
right?
> but someone has argued against this, saying he knows of no shell where the
> former is not acceptable. I realise this issue is probably more of a problem
> with older shells,
>
Solaris 10 /bin/sh is not really old.
> but can anyone give me any examples of where the former will
> break, but the latter will be ok?
>
HTH,
Stefano