Fun w/ test :)
Chris Larson
clarson at mvista.com
Mon Jul 21 23:15:55 UTC 2008
Denys Vlasenko wrote:
> Paul, hi. :)
>
> On Monday 21 July 2008 20:35, Chris Larson wrote:
>
>> clarson at foul busybox% ./busybox test -f =
>> clarson at foul busybox% ./busybox test \( -f = \)
>> test: closing paren expected
>>
>> Hehe. I highly doubt anyone would actually name a file =, but what the heck, figured you'd
>> want to know about any cases that confuse the parser. It's interesting to note that -f =
>> works, but ( -f = ) does not. No customer issue on this of course, just made myself try
>> lots of combinations during the testing.
>>
>> I found a case where the "unknown operand" error is seen. If it sees an operand after an
>> expression, which is not -o or -a, it gives that error:
>> clarson at foul busybox% ./busybox test -f = -a
>> clarson at foul busybox% ./busybox test -f = -a -e foo
>> test: -e: unknown operand
>>
>> Due to the precedence, that's seen as two expressions without a -a or -o to join them.
>> The error message could be confusing in this case. -e is, of course, a valid
>> operator, and -e foo is a valid expression, but -e is not valid -there-, only
>> -a or -o are.
>>
>
>
> So, now, do you see why x"str" trick is actually not so silly?
>
I now recall reading about this in the shell portability section of the
autoconf manual. Excerpt:
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 10.0.0.6, 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 ! "string"'.
--
Chris Larson
Dedicated Engineer
Montavista, Inc.
Email: clarson AT mvista DOT com
Email: clarson AT kergoth DOT com
More information about the busybox
mailing list