From owner-freebsd-questions@FreeBSD.ORG Mon Jun 10 19:20:28 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 CD1A28AF; Mon, 10 Jun 2013 19:20:28 +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 9EC331C6C; Mon, 10 Jun 2013 19:20:28 +0000 (UTC) Received: from smtp.fisglobal.com ([10.132.206.15]) by ltcfislmsgpa03.fnfis.com (8.14.5/8.14.5) with ESMTP id r5AJKR31013895 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT); Mon, 10 Jun 2013 14:20:27 -0500 Received: from dtwin (10.242.182.25) by smtp.fisglobal.com (10.132.206.15) with Microsoft SMTP Server (TLS) id 14.2.309.2; Mon, 10 Jun 2013 14:20:26 -0500 From: Sender: Devin Teske To: , References: <51b620a6.42f.2b6a6400.5605dcf0@go2france.com> <121701ce660c$9a9aa5b0$cfcff110$@freebsd.org> <51B62389.5000500@tundraware.com> <121b01ce660e$2abae5f0$8030b1d0$@freebsd.org> <51B62622.1070201@tundraware.com> In-Reply-To: <51B62622.1070201@tundraware.com> Subject: RE: Bourne shell "if" syntax Date: Mon, 10 Jun 2013 12:18:54 -0700 Message-ID: <123201ce660f$59a7b9f0$0cf72dd0$@freebsd.org> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Content-Language: en-us Thread-index: AQKqCa+a9hyR0ot3Jqq7BhaQloss/QGZ464NAYNXZdYBptsxcwJq94O4lz6SEpA= X-Originating-IP: [10.242.182.25] X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.10.8626, 1.0.431, 0.0.0000 definitions=2013-06-10_05:2013-06-10,2013-06-10,1970-01-01 signatures=0 Cc: dteske@freebsd.org X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 10 Jun 2013 19:20:28 -0000 > -----Original Message----- > From: owner-freebsd-questions@freebsd.org [mailto:owner-freebsd- > questions@freebsd.org] On Behalf Of Tim Daneliuk > Sent: Monday, June 10, 2013 12:17 PM > To: dteske@freebsd.org > Cc: freebsd-questions@freebsd.org > Subject: Re: Bourne shell "if" syntax > > On 06/10/2013 02:10 PM, dteske@freebsd.org wrote: > > > > > >> -----Original Message----- > >> From: owner-freebsd-questions@freebsd.org [mailto:owner-freebsd- > >> questions@freebsd.org] On Behalf Of Tim Daneliuk > >> Sent: Monday, June 10, 2013 12:06 PM > >> To: freebsd-questions@freebsd.org > >> Subject: Re: Bourne shell "if" syntax > >> > >> On 06/10/2013 01:59 PM, dteske@freebsd.org wrote: > >>> > >>> > >>>> -----Original Message----- > >>>> From: owner-freebsd-questions@freebsd.org [mailto:owner-freebsd- > >>>> questions@freebsd.org] On Behalf Of lconrad@go2france.com > >>>> Sent: Monday, June 10, 2013 11:53 AM > >>>> To: freebsd-questions@freebsd.org > >>>> Subject: Bourne shell "if" syntax > >>>> > >>>> > >>>> > >>>> script fragment: > >>>> > >>>> PTR=`dig @some.dns +short +norec -x a.b.c.d` > >>>> > >>>> echo "$PTR" > >>>> > >>>> if [ "$PTR" == "" ] ; then > >>>> > >>> > >>> if [ "$PTR" = "" ]; then > >>> > >>> or > >>> > >>> if [ -z "$PTR" ]; then > >>> > >>> or > >>> > >>> if [ "$PTR" ]; then > >>> > >>> but _NOT_ > >>> > >>> if [ "$PTR" == "" ]; then > >>> > >> > >> > >> I work across a bunch of different OSs and shells of many vintages. As I > > recall, > >> the -z argument has problems of portability on older/broken shells and/or > >> is not available in all environments (I cannot recall which at the moment). > > So > >> I achieve the same results by using a character sentinel that guarantees that > > the > >> comparison always works: > >> > >> f [ _"$PTR" == _ ] ; then > >> > > > > 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. > > > > > That wasn't really my point. I use sentinels because in the face of an > empty string this: > > if [ $PTR = "" ] > > Actually evaluates to: > > if [ = "" ] > and hence why you shouldn't do that. Instead do this: if [ "$PTR" = "" ] Which [potentially] evaluates to: if [ "" = "" ] > Which throws an error. The character sentinel avoids this without having to > use -z, which as I said, I've had problems with not being too portable across > older machinery. > Which again, is because you're not double-quoting your parameter. The sentinel is not required if you double-quote your parameter (which you were already doing in your example). For example (with sentinel): if [ _"$PTR" == (sic) _ ] ; then Since you've already double-quoted the parameter, I'm letting you know that the sentinel is unnecessary. -- Devin > > > All work as expected. It matters not the value of $foo. sh(1) in FreeBSD knows > > because of the double-quotes that it is not an operator. > > > > Furthermore... > > > > "==" is not the right operator. It's "=". > > > > Portability would surely be compromised if you were using "==" (which doesn't > > work on FreeBSD; or many other OSes I gather from experience). > > > > Ooops, I did catch that and you're quite right. > -- > ----------------------------------------------------------------------- > Tim Daneliuk > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org" _____________ 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.