• A Great friend to the HardForum with a great kid that he is trying to get a scholorship to continue his schooling. Please give hime a vote! Only 24 hours left! Thanks.
    If you have an VOTE FOR KEENAN!

bash help with expr

XOR != OR

[H]F Junkie
Joined
Jun 17, 2003
Messages
11,547
Hi all. I'm trying to write a script that will only run hourly between 7am and 7pm. To that end, I want to use expr to evalutate whether the current hour falls in that range. I am experiencing some odd behavior however.

Code:
[user@server documents]# if [ `expr 3 < 2` ]; then echo "hi there"; fi
hi there
[user@server documents]# 
...
[user@server documents]# if [ `expr 3 > 2` ]; then echo "hi there"; fi
[user@server documents]#

It would seem that expr evaluates to 1 if the expression is false? That doesn't make sense to me, but I'm not used to bash programing. Any advice is apprecaited.
 
I am not sure why it is like that, but it is. When using Operators with expr, 1 = false and 0 = true. Totally backwards from normal thought of course :)

Sort of OT, but why not simply setup a cron job to run at the proper times? (just curious)
 
Tweakin said:
I am not sure why it is like that, but it is. When using Operators with expr, 1 = false and 0 = true. Totally backwards from normal thought of course :)

Sort of OT, but why not simply setup a cron job to run at the proper times? (just curious)
Wasn't sure how to do it, actually. I've peaked at crontab, but I figured this would be an easier solution. Just encase my code in a couple if statements, and I'm gold.

Could be going the long way about it, wouldn't be the first time.
 
I think you're not really saying what you think you're saying...
Code:
/bin/foo bar > baz
is handled a little funny..

You're probably want to say something like...
Code:
[DEVL] /u02/home/smckeon1>if [ `expr 3 \< 2` ]; then echo "hi there"; fi
hi there
[DEVL] /u02/home/smckeon1>if [ `expr 3 \> 2` ]; then echo "hi there"; fi
hi there
...which doesn't do what you want. Why is this?

[ evaluates -return- values, while expr puts results on stdout. I think what you're really trying to do is something like ...
Code:
if [ 3 -gt 2 ]; then echo "hi there"; fi
 
ameoba said:
[ evaluates -return- values, while expr puts results on stdout. I think what you're really trying to do is something like ...
Code:
if [ 3 -gt 2 ]; then echo "hi there"; fi
Bah! I knew it was something that simple.

Thanks!
 
XOR != OR said:
Hi all. I'm trying to write a script that will only run hourly between 7am and 7pm. To that end, I want to use expr to evalutate whether the current hour falls in that range. I am experiencing some odd behavior however.

Code:
7,8,9,10,11,12,13,14,15,16,17,18,19 * * * * /usr/local/bin/yourscript.sh
(Err, without the gap between 17, and 18. That's VB messing up.)

You can use: `date +%H` to get the current hour as well.


Definitely use bash test syntax as amoeba stated.
 
doh said:
Code:
7,8,9,10,11,12,13,14,15,16,17,18,19 * * * * /usr/local/bin/yourscript.sh
(Err, without the gap between 17, and 18. That's VB messing up.)

You can use: `date +%H` to get the current hour as well.


Definitely use bash test syntax as amoeba stated.

Code:
0 7-19 * * * /path/to/script.sh

That's what I would come up with, is there a reason you leave the minute off (maybe it isn't needed)?
 
Tweakin said:
Code:
0 7-19 * * * /path/to/script.sh

That's what I would come up with, is there a reason you leave the minute off (maybe it isn't needed)?

Brain fart. :D I always get the order wrong. I didn't think the range worked there, but that's brilliant.
 
Back
Top