Bourne shell "if" syntax

dteske at freebsd.org dteske at freebsd.org
Mon Jun 10 19:22:59 UTC 2013



> -----Original Message-----
> From: Devin Teske [mailto:devin.teske at fisglobal.com] On Behalf Of
> dteske at freebsd.org
> Sent: Monday, June 10, 2013 12:19 PM
> To: tundra at tundraware.com; freebsd-questions at freebsd.org
> Cc: dteske at freebsd.org
> Subject: RE: Bourne shell "if" syntax
> 
> 
> 
> > -----Original Message-----
> > From: owner-freebsd-questions at freebsd.org [mailto:owner-freebsd-
> > questions at freebsd.org] On Behalf Of Tim Daneliuk
> > Sent: Monday, June 10, 2013 12:17 PM
> > To: dteske at freebsd.org
> > Cc: freebsd-questions at freebsd.org
> > Subject: Re: Bourne shell "if" syntax
> >
> > On 06/10/2013 02:10 PM, dteske at freebsd.org wrote:
> > >
> > >
> > >> -----Original Message-----
> > >> From: owner-freebsd-questions at freebsd.org [mailto:owner-freebsd-
> > >> questions at freebsd.org] On Behalf Of Tim Daneliuk
> > >> Sent: Monday, June 10, 2013 12:06 PM
> > >> To: freebsd-questions at freebsd.org
> > >> Subject: Re: Bourne shell "if" syntax
> > >>
> > >> On 06/10/2013 01:59 PM, dteske at freebsd.org wrote:
> > >>>
> > >>>
> > >>>> -----Original Message-----
> > >>>> From: owner-freebsd-questions at freebsd.org [mailto:owner-freebsd-
> > >>>> questions at freebsd.org] On Behalf Of lconrad at go2france.com
> > >>>> Sent: Monday, June 10, 2013 11:53 AM
> > >>>> To: freebsd-questions at 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.
> 

Actually, there's another reason you should also avoid the above (unquoted
parameter), and that's in the case of a multi-word value. For example:

foo="abc 123"
if [ $foo = "" ]; then

Produces:

sh: line 0: [: too many arguments

-- 
Devin


> 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 at freebsd.org mailing list
> > http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> > To unsubscribe, send any mail to "freebsd-questions-
> unsubscribe at 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.

_____________
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