User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 456,572 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,589 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 2174 | Replies: 4 | Solved
Reply
Join Date: Oct 2007
Posts: 22
Reputation: dave_nithis is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dave_nithis dave_nithis is offline Offline
Newbie Poster

Question Compare two dates using Shell Programming

  #1  
Oct 26th, 2007
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,518
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is online now Online
Posting Virtuoso

Re: Compare two dates using Shell Programming

  #2  
Oct 26th, 2007
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.
Last edited by masijade : Oct 26th, 2007 at 9:57 am. Reason: Oops, wrong var names in the comments
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Oct 2007
Posts: 22
Reputation: dave_nithis is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dave_nithis dave_nithis is offline Offline
Newbie Poster

Re: Compare two dates using Shell Programming

  #3  
Oct 26th, 2007
I am new to this shell scripting..
Can you give me the comparision part also

Regards
Dave Nithis
Reply With Quote  
Join Date: Feb 2006
Posts: 1,518
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is online now Online
Posting Virtuoso

Re: Compare two dates using Shell Programming

  #4  
Oct 26th, 2007
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.
Last edited by masijade : Oct 26th, 2007 at 12:48 pm. Reason: typo
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Apr 2006
Posts: 140
Reputation: ghostdog74 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 26
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Compare two dates using Shell Programming

  #5  
Oct 28th, 2007
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"}
}
' 
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 6:06 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC