From: "Phil B" phil.remove.brady on
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


From: Lew Pitcher on
On January 15, 2010 12:51, in alt.os.linux, 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?

In this case, $hour (which /is/ a string) is re-evaluated as an integer, and
compared to (the integer) 18

First off, remember that Bash doesn't have a builtin [ test operator; the [
in your if statement is actually /bin/[, which is link to /bin/test. The
builtin test operator in bash is [[ (with it's corresponding ]]
terminator).

So, your Bash if statement is actually running /bin/[ (/bin/test) to
evaluate the test condition.

The manpage for /bin/[ and /bin/test says:
INTEGER1 -gt INTEGER2
INTEGER1 is greater than INTEGER2
and that matches what you've coded.

--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------


From: Bit Twister on
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
From: Aragorn on
On Friday 15 January 2010 19:40 in alt.os.linux, somebody identifying as
Lew Pitcher wrote...

> On January 15, 2010 12:51, in alt.os.linux, 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?
>
> In this case, $hour (which /is/ a string) is re-evaluated as an
> integer, and compared to (the integer) 18
>
> First off, remember that Bash doesn't have a builtin [ test operator;

I'm afraid that's not true, Lew. ;-)

> the [ in your if statement is actually /bin/[, which is link to
> /bin/test. The builtin test operator in bash is [[ (with it's
> corresponding ]] terminator).
>
> So, your Bash if statement is actually running /bin/[ (/bin/test) to
> evaluate the test condition.

[aragorn] $> type [
[ is a shell builtin

:-)
--
*Aragorn*
(registered GNU/Linux user #223157)
From: Lew Pitcher on
On January 15, 2010 13:51, in alt.os.linux, aragorn(a)chatfactory.invalid
wrote:

> On Friday 15 January 2010 19:40 in alt.os.linux, somebody identifying as
> Lew Pitcher wrote...
>
>> On January 15, 2010 12:51, in alt.os.linux, 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?
>>
>> In this case, $hour (which /is/ a string) is re-evaluated as an
>> integer, and compared to (the integer) 18
>>
>> First off, remember that Bash doesn't have a builtin [ test operator;
>
> I'm afraid that's not true, Lew. ;-)
[snip]
> [aragorn] $> type [
> [ is a shell builtin
>
> :-)


Oops...

OK. Then I missed it in the manpage.

OTOH, the shell builtin [ seems to work the same way that /bin/[ works, and
the manpage for /bin/[ is good documentation for it.

--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------