[ale] Bash confusion
Al Snow
jasnow at hotmail.com
Wed Jul 20 15:29:42 EDT 2011
Try if [ "X01" = "X${date}" ] then if $date is empty you still have "X".
Al
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.ale.org/pipermail/ale/attachments/20110720/955f6112/attachment.html
More information about the Ale
mailing list