954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Compare two dates using Shell Programming

Hi all,

a=2007-05-10
b=2007-06-10

These are the two given dates and I need to compare.

The script should first compare years.If both are same or if a is lesser than b,it should print correct.If a is greater than b,then it should print error.Similarly for the dates(05 and 06).

No need to check for the months(10)

Regards
Dave Nithis

dave_nithis
Newbie Poster
23 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

are you using ksh or bash? if so

set -A aArray `print "$a" | sed -e 's/-/ /'`
set -A bArray `print "$b" | sed -e 's/-/ /'`
# now ${aArray[0]} and ${bArray[0]} contain the year
# and ${aArray[1]} and ${bArray[1]} contain the day
# and ${aArray[2]} and ${bArray[2]} contain the month


The comparison parts you can do yourself.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

I am new to this shell scripting..
Can you give me the comparision part also

Regards
Dave Nithis

dave_nithis
Newbie Poster
23 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

At the top of this forum is a fixed post to a tutorial. Read through some of that, try something out, if it doesn't work post your code and I'll help, but I'll not do it for you.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 
a="2007-05-10"
b="2007-06-10"
awk -v a="$a" -v b="$b" 'BEGIN{
n=split(a,tmpa,"-")
m=split(b,tmpb,"-")
if ( ( tmpa[1] <= tmpb[1] )  && ( tmpa[2] <= tmpb[2] ) ){
 print "correct"
}
else { print "not correct"}
}
'
ghostdog74
Junior Poster
156 posts since Apr 2006
Reputation Points: 75
Solved Threads: 44
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You