Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

compare dates using shell sript

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-13-2011   -   Original Discussion by LavanyaP
LavanyaP's Unix or Linux Image
LavanyaP LavanyaP is offline
Registered User
 
Join Date: May 2011
Last Activity: 12 October 2011, 2:03 AM EDT
Posts: 17
Thanks: 4
Thanked 0 Times in 0 Posts
compare dates using shell sript

I have 2 date feilds


Code:
2011-05-13:18:45
2011-05-13:18:30

I need to compare them and say its OK/NOK

I tried this but dint work.


Code:
systime=2011-05-13:18:45
shubtime=2011-05-13:18:30
if [ "$systime" -eq "$shubtime" ]
then
         echo" OK"
else
         echo "NOK"
fi

In this its not same so the o/p should be NOK

Last edited by Scott; 05-13-2011 at 07:35 PM.. Reason: Added code tags
Sponsored Links
    #2  
Old Unix and Linux 05-13-2011   -   Original Discussion by LavanyaP
ctsgnb's Unix or Linux Image
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 3 April 2018, 6:42 AM EDT
Location: France
Posts: 2,967
Thanks: 88
Thanked 640 Times in 610 Posts


Code:
ksh
systime=2011-05-13:18:45
shubtime=2011-05-13:18:45
echo $systime $shubtime | tr -d ':-' | read a b
[[ $a -eq $b ]] && echo OK || echo KO

The Following User Says Thank You to ctsgnb For This Useful Post:
LavanyaP (05-18-2011)
Sponsored Links
    #3  
Old Unix and Linux 05-13-2011   -   Original Discussion by LavanyaP
frans's Unix or Linux Image
frans frans is offline Forum Advisor  
Registered User
 
Join Date: Oct 2009
Last Activity: 1 February 2016, 9:47 AM EST
Location: France
Posts: 839
Thanks: 4
Thanked 54 Times in 53 Posts
If they must be exactly the same


Code:
systime=2011-05-13:18:45
shubtime=2011-05-13:18:30
if [ "$systime" = "$shubtime" ]
then
    echo" OK"
else
    echo "NOK"
fi

Or shorter:

Code:
[ "$systime" = "$shubtime" ] && echo" OK" || echo "NOK"

PS: please use code tags and indent your code
The Following User Says Thank You to frans For This Useful Post:
LavanyaP (05-18-2011)
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
convert the below perl sript to shell script mail2sant Shell Programming and Scripting 1 04-04-2008 01:36 PM
Shell Sript jazz8146 UNIX for Dummies Questions & Answers 3 01-11-2008 01:07 PM
Compare two dates using Shell Programming dave_nithis Shell Programming and Scripting 4 01-04-2008 04:18 AM
Shell Sript - Spell Checker Assign ccfc1986 Shell Programming and Scripting 9 11-14-2007 11:52 PM
How to compare the dates in shell script vaji Shell Programming and Scripting 9 02-27-2007 11:34 PM



All times are GMT -4. The time now is 04:23 PM.