From: "Phil B" phil.remove.brady on

"Bit Twister" <BitTwister(a)mouse-potato.com> wrote in message
news:slrnhl1e3u.fjg.BitTwister(a)wb.home.test...
> On Fri, 15 Jan 2010 17:51:43 -0000, Phil B wrote:
>> Would someone help this newbie out please? I'm trying to detect 'after
>> 6pm' but it is failing the if condition.
>> In it's simplest form it is:
>>
>> #!/bin/bash
>> hour=$(date +%H)
>> if [ "$hour" -gt 18 ] ; then
>> echo it is evening
>> fi
>>
>> I think that $hour is treated as a string - how should I code this?
>
> It works without or without quotes for me. I cut/pasted your script
> for testing from the command line and changed 18 to 11 for the test.
>
>
> $ echo $SHELL
> /bin/bash
>
> $ date
> Fri Jan 15 12:41:27 CST 2010
>
> $ hour=$(date +%H)
>
> $ if [ "$hour" -gt 11 ] ; then echo it is evening; fi
> it is evening
>
> $ if [ $hour -gt 11 ] ; then echo it is evening; fi
> it is evening
>
>
> Feel free to use the set command to help debug your script. Example:
>
> #!/bin/bash
>
> set -x
>
> hour=$(date +%H)
> if [ "$hour" -gt 18 ] ; then
> echo it is evening
> fi
>
> set -
> #************** end test script ****************
>
> Other set args of interest -v -u
>
> Some light reading found here
> http://tldp.org/LDP/abs/html/index.html
> http://mywiki.wooledge.org/BashFAQ/050
> http://cfaj.freeshell.org/shell

Thanks Bit Twister, You're quite right and thanks for restoring sanity.
It was of course too late in the night when I was trying a more complex
script - not thinking straight.
thanks again
Phil




From: Jasen Betts on
On 2010-01-15, Phil B <phil.remove.brady(a)hotmail> wrote:
> Would someone help this newbie out please? I'm trying to detect 'after
> 6pm' but it is failing the if condition.
> In it's simplest form it is:
>
> #!/bin/bash
> hour=$(date +%H)
> if [ "$hour" -gt 18 ] ; then
> echo it is evening
> fi
>
> I think that $hour is treated as a string - how should I code this?


I would do it using an arithmetic expression.

Not that there's anything wrong with using [ (or test) for this check,
it's just that as a C programmer I find the arithmetic syntax more
"natural", and the operators easier to remeber.

#!/bin/bash
hour=$(date +%H)
if (( hour > 18 ))
then
echo it is evening
fi

--- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---
From: Martin on
"Phil B" <phil.remove.brady(a)hotmail dot co dot united kingdom> wrote:

> Would someone help this newbie out please? I'm trying to detect 'after
> 6pm' but it is failing the if condition.
> In it's simplest form it is:
>
> #!/bin/bash
> hour=$(date +%H)
> if [ "$hour" -gt 18 ] ; then
> echo it is evening
> fi
>
> I think that $hour is treated as a string - how should I code this?
>
> Regards
> Phil

This may not apply to your application, but when doing more sophisticated
stuff (particularly with strings) it sometimes pays using Perl as scripting
engine (from the point of view of performance and development efficiency).
Your script would look like this:

#!/usr/bin/perl
my $hour = (localtime)[2];
if ($hour > 18) {
printf("It is evening\n");
}

Martin

From: Florian Diesch on
"Phil B" <phil.remove.brady(a)hotmail dot co dot united kingdom> writes:

> Would someone help this newbie out please? I'm trying to detect 'after
> 6pm' but it is failing the if condition.
> In it's simplest form it is:
>
> #!/bin/bash
> hour=$(date +%H)
> if [ "$hour" -gt 18 ] ; then
> echo it is evening
> fi

Works for me - but of course it means "at least 7pm".



Florian
--
<http://www.florian-diesch.de/>