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.
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.
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:
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