Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 11 Jun 2013 14:46:37 +0000
From:      "Teske, Devin" <Devin.Teske@fisglobal.com>
To:        Jan Henrik Sylvester <me@janh.de>
Cc:        Devin Teske <dteske@FreeBSD.org>, questions-list freebsd <freebsd-questions@freebsd.org>
Subject:   Re: Bourne shell "if" syntax
Message-ID:  <13CA24D6AB415D428143D44749F57D7201F7C956@ltcfiswmsgmb21>
In-Reply-To: <51B6F459.5050306@janh.de>
References:  <51b620a6.42f.2b6a6400.5605dcf0@go2france.com> <121701ce660c$9a9aa5b0$cfcff110$@freebsd.org> <51B62389.5000500@tundraware.com> <121b01ce660e$2abae5f0$8030b1d0$@freebsd.org> <51B6F459.5050306@janh.de>

next in thread | previous in thread | raw e-mail | index | archive | help

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

> On 06/10/2013 21:10, dteske@freebsd.org wrote:
>> Character sentinels are not required.
>>=20
>> 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 ope=
rator.
>=20

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-br=
acket).

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=3D-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 bu=
t 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:
>=20
> $ A=3D-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
> $
>=20

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

devin@mick.vicor.com ~ $ if /bin/[ "$A" "" ] ; then echo z ; fi
[: ]: unknown operand
devin@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 th=
e ending square-bracket it runs=85

devin@mick.vicor.com ~ $ if /bin/[ "$A" "" ; then echo z ; fi
z
devin@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.
>=20

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

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

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

=3D=3D=3D

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

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

z

--=20
Devin

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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?13CA24D6AB415D428143D44749F57D7201F7C956>