Bourne shell "if" syntax

Teske, Devin Devin.Teske at fisglobal.com
Tue Jun 11 14:46:52 UTC 2013


On Jun 11, 2013, at 2:56 AM, Jan Henrik Sylvester wrote:

> On 06/10/2013 21:10, dteske at freebsd.org wrote:
>> Character sentinels are not required.
>> 
>> FreeBSD's sh(1) knows (because "[" is a built-in) that when you quote a
>> parameter, that it is not (even if the value begins with "-") not an operator.
> 

Appears I was wrong (and I can admit that).

Further testing/investigation shows that it's not based on double-quoting (although this DOES play a role -- but only in the fact that you're ensuring that the program -- [ -- only gets one argument -- sans trailing square-bracket).

It appears that the divide is the number of arguments.

For example, compare:

if [ "-gt 1" ]; then # arg1 treated as a string; returns success

to

if [ "-gt" "1" ]; then # arg1 treated as an operator; syntax error

to

if [ "-gt" ]; then # arg1 treated as a string; returns success


Or compare the equally divided:

A=-gt

if [ "$A 1" ]; then # arg1 treated as a string; returns success

to

if [ "$A" "1" ]; then # arg1 treated as an operator; syntax error

to

if [ "$A" ]; then # arg1 treated as a string; returns success

When the program -- [ -- (regardless of built-in or external) sees only one argument in it's ARGV array, it will *not* treat the argument as a flag but instead return true if it's non-NULL or return false if NULL.





> What you are saying here is at least misleading. I just started up sh on
> 9.1 RELEASE and tried:
> 
> $ A=-z
> $ if [ "$A" "" ] ; then echo z ; fi
> z
> $ if [ "$A" "1" ] ; then echo z ; fi
> $ if /bin/[ "$A" "" ] ; then echo z ; fi
> z
> $ if /bin/[ "$A" "1" ] ; then echo z ; fi
> $
> 

Interesting that I do not get the same results on 9.0-R for the external usage.

devin at mick.vicor.com ~ $ if /bin/[ "$A" "" ] ; then echo z ; fi
[: ]: unknown operand
devin at mick.vicor.com ~ $ if /bin/[ "$A" "1" ] ; then echo z ; fi
[: ]: unknown operand

Must be a bug in 9.0-R with the ending square-bracket (`]'). If I remove the ending square-bracket it runs…

devin at mick.vicor.com ~ $ if /bin/[ "$A" "" ; then echo z ; fi
z
devin at mick.vicor.com ~ $ if /bin/[ "$A" 1 ; then echo z ; fi





> Although "-z" is quoted, it is seen as an operator. It does not seem to
> have anything to do with whether the build-in or external "[" is used.
> 

You're at least partially right… number of arguments makes a difference.

A=-gt
if [ "$A" "" ]; then echo z; fi

sh: line 0: [: -gt: unary operator expected

===

Meanwhile, it knows to treat it as a string when it's the only argument…

A=-gt
if [ "$A" ]; then echo z; fi

z

-- 
Devin

_____________
The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you.


More information about the freebsd-questions mailing list