|
|||||||||
| 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. |
learn linux and unix commands - unix shell scripting |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
Hi all,
a=2007-05-10 (YYYY-DD-MM Format) b=2007-06-10 These are the two given dates and I need to compare. (First It should split the dates into YYYY,dd,mm) The script should first compare years(2007 here).If both are same or if "a" is lesser than "b"(ie.suppose year in "a" is 2006),it should print correct.If "a"(ie. suppose year in a is 2008) 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. |
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
convert the dates to a long integer (ksh) and then subtract Code:
#!/bin/ksh a="2007-10-27" b="2006-03-22" if [[ $(( $(echo $a | tr -d '-') - $(echo $b | tr -d '-') )) -ge 0 ]] ; then print "correct" else print "failure" fi |
|
#4
|
||||
|
||||
|
in bash you could do
(( ${a:0:4} <= ${b:0:4} )) && echo true || echo false or if you want to check year AND day (( ${a:0:4} <= ${b:0:4} && ${a:5:2} <= ${b:5:2} )) && echo true|| echo false |
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
Can you explain the if part?
|
| Sponsored Links | ||
|
|
![]() |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Compare Dates. | Nagaraja Akkiva | Shell Programming and Scripting | 4 | 08-07-2011 02:08 AM |
| compare dates using shell sript | LavanyaP | Shell Programming and Scripting | 2 | 05-13-2011 06:15 PM |
| compare between the two dates | balaji23_d | Shell Programming and Scripting | 1 | 01-08-2009 07:47 AM |
| Compare one file with 7 files in shell programming | ravi214u | Shell Programming and Scripting | 4 | 07-07-2008 11:08 AM |
| How to compare the dates in shell script | vaji | Shell Programming and Scripting | 9 | 02-28-2007 12:34 AM |
|
|