From owner-freebsd-questions@FreeBSD.ORG Tue Jun 11 14:46:52 2013 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id B22812A7; Tue, 11 Jun 2013 14:46:52 +0000 (UTC) (envelope-from Devin.Teske@fisglobal.com) Received: from mx1.fisglobal.com (mx1.fisglobal.com [199.200.24.190]) by mx1.freebsd.org (Postfix) with ESMTP id 81AC71859; Tue, 11 Jun 2013 14:46:52 +0000 (UTC) Received: from smtp.fisglobal.com ([10.132.206.17]) by ltcfislmsgpa02.fnfis.com (8.14.5/8.14.5) with ESMTP id r5BEkLVf006194 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT); Tue, 11 Jun 2013 09:46:50 -0500 Received: from LTCFISWMSGMB21.FNFIS.com ([10.132.99.23]) by LTCFISWMSGHT06.FNFIS.com ([10.132.206.17]) with mapi id 14.02.0309.002; Tue, 11 Jun 2013 09:46:37 -0500 From: "Teske, Devin" To: Jan Henrik Sylvester Subject: Re: Bourne shell "if" syntax Thread-Topic: Bourne shell "if" syntax Thread-Index: AQHOZgydcuJSc31L6EmVLEMa2+THwpkvoOsAgAAB0oCAAAFPAIAA952AgABRAoA= Date: Tue, 11 Jun 2013 14:46:37 +0000 Message-ID: <13CA24D6AB415D428143D44749F57D7201F7C956@ltcfiswmsgmb21> 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> In-Reply-To: <51B6F459.5050306@janh.de> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.132.253.126] Content-Type: text/plain; charset="Windows-1252" Content-ID: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.10.8794, 1.0.431, 0.0.0000 definitions=2013-06-11_06:2013-06-11,2013-06-11,1970-01-01 signatures=0 Cc: Devin Teske , questions-list freebsd X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: Devin Teske List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 11 Jun 2013 14:46:52 -0000 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.