From owner-freebsd-questions@FreeBSD.ORG Mon Jun 10 19:17:00 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 A01296A9; Mon, 10 Jun 2013 19:17:00 +0000 (UTC) (envelope-from tundra@tundraware.com) Received: from ozzie.tundraware.com (ozzie.tundraware.com [75.145.138.73]) by mx1.freebsd.org (Postfix) with ESMTP id 62AE71C35; Mon, 10 Jun 2013 19:17:00 +0000 (UTC) Received: from [10.219.130.119] ([66.175.245.1]) (authenticated bits=0) by ozzie.tundraware.com (8.14.7/8.14.7) with ESMTP id r5AJGYtn024643 (version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO); Mon, 10 Jun 2013 14:16:34 -0500 (CDT) (envelope-from tundra@tundraware.com) Message-ID: <51B62622.1070201@tundraware.com> Date: Mon, 10 Jun 2013 14:16:50 -0500 From: Tim Daneliuk Organization: TundraWare Inc. User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130329 Thunderbird/17.0.5 MIME-Version: 1.0 To: dteske@freebsd.org Subject: Re: Bourne shell "if" syntax References: <51b620a6.42f.2b6a6400.5605dcf0@go2france.com> <121701ce660c$9a9aa5b0$cfcff110$@freebsd.org> <51B62389.5000500@tundraware.com> <121b01ce660e$2abae5f0$8030b1d0$@freebsd.org> In-Reply-To: <121b01ce660e$2abae5f0$8030b1d0$@freebsd.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.4.3 (ozzie.tundraware.com [75.145.138.73]); Mon, 10 Jun 2013 14:16:35 -0500 (CDT) X-TundraWare-MailScanner-Information: Please contact the ISP for more information X-TundraWare-MailScanner-ID: r5AJGYtn024643 X-TundraWare-MailScanner: Found to be clean X-TundraWare-MailScanner-From: tundra@tundraware.com X-Spam-Status: No Cc: freebsd-questions@freebsd.org X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: tundra@tundraware.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 10 Jun 2013 19:17:00 -0000 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 [ = "" ] 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. > 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