need help with test condition in shell script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

need help with test condition in shell script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-07-2006
pieman8080 pieman8080 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 23 May 2007, 5:34 PM EDT
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
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?
Sponsored Links
    #2  
Old 09-08-2006
Andrek Andrek is offline
Registered User
 
Join Date: Aug 2006
Last Activity: 27 April 2010, 9:08 PM EDT
Posts: 122
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #3  
Old 09-08-2006
pieman8080 pieman8080 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 23 May 2007, 5:34 PM EDT
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
thank u very much. i will try this and let u know the result.
    #4  
Old 09-08-2006
pieman8080 pieman8080 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 23 May 2007, 5:34 PM EDT
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
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` ]
Sponsored Links
    #5  
Old 09-08-2006
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 18 December 2014, 8:26 AM EST
Location: Bangalore,India
Posts: 2,096
Thanks: 12
Thanked 132 Times in 127 Posts
it should be || not |
Sponsored Links
    #6  
Old 09-08-2006
pieman8080 pieman8080 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 23 May 2007, 5:34 PM EDT
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
that worked! thank u both for your help. I'll let this run a few days and make sure it's stable.
Sponsored Links
    #7  
Old 09-11-2006
pieman8080 pieman8080 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 23 May 2007, 5:34 PM EDT
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to check weather a string is like test* or test* ot *test* in if condition johnjerome Shell Programming and Scripting 5 02-18-2010 06:38 AM
test condition tostay2003 Shell Programming and Scripting 1 12-03-2008 02:25 PM
Shell script - If---fi condition yog_chavan Shell Programming and Scripting 4 05-07-2008 10:46 AM
Condition test felixwhoals UNIX for Dummies Questions & Answers 2 12-22-2007 08:47 AM
test and if condition ciroredz Shell Programming and Scripting 9 06-27-2007 10:17 AM



All times are GMT -4. The time now is 12:24 AM.