[ale] Bash confusion

The Don Lachlan ale-at-ale.org at unpopularminds.org
Wed Jul 20 16:02:25 EDT 2011


On Wed, Jul 20, 2011 at 03:29:42PM -0400, Al Snow wrote:
> Try if [ "X01" = "X${date}" ] then if $date is empty you still have "X".

That is unnecessary and confusing.

  if [ "01" = "$date" ]

is simpler and more clear.

-L

> > Date: Wed, 20 Jul 2011 15:22:40 -0400
> > From: ale-at-ale.org at unpopularminds.org
> > To: ale at ale.org
> > Subject: Re: [ale] Bash confusion
> > 
> > On Wed, Jul 20, 2011 at 01:04:10PM -0400, Brian MacLeod wrote:
> > > You are doing a string comparison here, so you should take the correct
> > > precautions:
> > > > if [ $date = "01" ];
> > > should be like this:
> > > if [ "01" = "$date" ];
> > > Why?
> > > If for some ungodly reason your variable ends up as null, the whole
> > > statement (and function, etc) breaks if you have that null in front of
> > > the equal sign.  Flip that around and it "properly" evaluates even if
> > > null. Otherwise you'll see things like "unary operator" and such.
> > > The other thing is making sure you treat your variable as a string,
> > > thus, quote it so that you are sure it isn't being further evaluated
> > > and/or evaluated differently.
> > 
> > Order is irrelevent; quoting the string protects against null values.
> > 
> >   if [ "$date" = "01" ]
> > 
> > is safe, while
> > 
> >   if [ "01" = $date ]
> > 
> > fails if $date is a null string.
> > 
> > 
> > Regarding the original problem, as written, yes, it should work. But...
> > 
> > Are there any commands between the date command and the test? There's no
> > explicit path used to date - could it be executing a different version of
> > date which is evaluating improperly? IOW, what are you telling us? ;)
> > 
> > -L
> > _______________________________________________
> > Ale mailing list
> > Ale at ale.org
> > http://mail.ale.org/mailman/listinfo/ale
> > See JOBS, ANNOUNCE and SCHOOLS lists at
> > http://mail.ale.org/mailman/listinfo
>  		 	   		  
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo



More information about the Ale mailing list