need help with test condition in shell script

I'm new to scripting and I need help with a bourn shell script. What i'm trying to do is a test condition where "if the time is within 2 hours, it's true" and so on.

The time is in the following format

DATE=`/bin/date +"%Y%m%d%H%S"`

for example, 20060907152000.

So, what the script first does is go to a web page and the web page returns a number in that exact format. I have the script set up to go to the web page and look at that line.

What I need is help with a test condition to have it check to see if the variable from the web is within 2 hours of the system time and I have no idea how to do that.

Can someone help me?

This should work even if the clock ticks past midnight.....

timenow=20060907150000 # Your unix time
timeweb=20060907160000 # The time you got from the web
timediff=020000 # your 2 hour difference
if [ $timeweb -le `expr $timenow + $ timediff` ] | [ $timeweb -ge `expr $timenow - $timediff` ]
then
echo "Its a GOOD time :-)"
else
echo "Its a BAD time!"
fi

Not fully tested as you are adding and subtracting number with 14 digits, that could be a little big for UNIX, UNIX should roll it over.

You need to test it over a period to ensure it works for you.

The other method is to use "subtr" and exctract only the HHMMSS. The only issue with that is you will need to be aware of the clock changing days at midnight.

eg expr substr 20060907235900 9 6 gives 235900 while
expr substr 20060908000000 9 6 gives 000000

Haveagoodweekend

thank u very much. i will try this and let u know the result.

I think i'm almost there. I modified my script like u said and i'm getting error:

./2test.sh: line 24: [: 20060908112001: unary operator expected

the line that is failing is:

if [ $timeweb -le `expr $timenow + $timediff` ] | [ $timeweb -ge `expr $timenow - $timediff` ]

it should be || not |

that worked! thank u both for your help. I'll let this run a few days and make sure it's stable.

ok, so i thought this worked, but the test is always passing, no matter what the time is. I tested it by making the "timenow" variable 4 hours from the current time and it doesnt' fail like it should. It shoud fail if it's off by more than 2 hours. Here's my script right now, what exactly is that "if" line saying? and how do i change it to what i want:

timenow=20060907150000 # Your unix time
timeweb=20060907160000 # The time you got from the web
timediff=020000 # your 2 hour difference
if [ $timeweb -le `expr $timenow + $ timediff` ] || [ $timeweb -ge `expr $timenow - $timediff` ]
then
echo "Its a GOOD time :-)"
else
echo "Its a BAD time!"
fi

I think I figured out that the || should actually be && since I need both expresions to be true. But when I change it, I'm getting the following error on that line:

expr: syntax error
[: 20060911112000: unary operator expected

try this

if [ `expr $timeweb - $timenow` -le $timediff ] && [ `expr $timeweb - $timenow` -ge -$timediff ]

in your previous code the value of this `expr $timenow + $ timediff` exceeds the range and you will get negative number. thats why code gives wrong result

thanks again. I actually had to do the following:

if [ `expr $timeweb - $timenow` -le $timediff ] && [ `expr $timenow- $timeweb` -le -$timediff ]