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

_______________________________________________
Autoconf mailing list
Autoconf@gnu.org
http://lists.gnu.org/mailman/listinfo/autoconf

Reply via email to