[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: What shells fail to work if comparing with "" ?
From: |
Eric Blake |
Subject: |
Re: What shells fail to work if comparing with "" ? |
Date: |
Thu, 10 Feb 2011 12:52:40 -0700 |
User-agent: |
Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101209 Fedora/3.1.7-0.35.b3pre.fc14 Lightning/1.0b3pre Mnenhy/0.8.3 Thunderbird/3.1.7 |
On 02/10/2011 11:56 AM, Dr. David Kirkby 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?
At least Solaris /bin/sh mishandles particular $STR:
$ /bin/sh -c 'test \( = ""'; echo $?
/bin/sh: test: argument expected
1
Do you really want to be polluting stderr if $STR happens to be "("?
>
>
> I was proposing someone change a test like that to
>
> if [ "x$STR" = ] ; then
Won't work. With only two arguments, that provokes syntax errors in
most versions of test.
Did you mean:
[ -z "$STR" ]
or
[ ! "$STR" ]
If so, that still won't work, as there are some test implementations
that get order of precedence wrong for some $STR.
Did you mean:
[ "x$STR" = x ]
if so, then you can see why that is the exact same recipe that the
autoconf manual is adamant about recommending, since the three-argument
form with a prefix (usually x) that guarantees that the first and last
argument can't be misinterpreted as operations and cause spurious syntax
errors.
> but someone has argued against this, saying he knows of no shell where
> the former is not acceptable.
Well, then he doesn't know about Solaris /bin/sh; there are other broken
shells still in the wild, as well.
> I realise this issue is probably more of a
> problem with older shells, but can anyone give me any examples of where
> the former will break, but the latter will be ok?
Still very much a problem to be aware of with today's portable shell
programming.
--
Eric Blake address@hidden +1-801-349-2682
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature