Hi
Need some function or step to compare the date as given below.
Example:
Date_1: 25/04/2013
Date_2: 20/07/2012
if Date_1 is greater than Date_2 then
do...
else
do..
fi
Need exact unix steps to compare the above condition
Hi
Need some function or step to compare the date as given below.
Example:
Date_1: 25/04/2013
Date_2: 20/07/2012
if Date_1 is greater than Date_2 then
do...
else
do..
fi
Need exact unix steps to compare the above condition
This is quite often being asked here in the forums, so I suggest you either use the search function for exactly the text your subject contains or just scroll down the bottom of the thread to see the automatically suggested relating threads to your problem.
i couldnt find any relevant answers, hence posted. some body please help.
i used
#! /bin/sh
olddate=`21/07/2013`
newdate=`22/07/2013`
if [ $olddate -gt $newdate ];then
echo "olddate"
else
echo "newdate"
fi
but the above doesnt work out.
Convert the dates to YYYYMMDD first...
#! /bin/sh
olddate="21/07/2013"
newdate="22/07/2013"
# Change date string to yyyymmdd format...
olddate=`echo $olddate | awk -F"/" '{printf "%0.4d%0.2d%0.2d\n",$3,$2,$1}'`
newdate=`echo $newdate | awk -F"/" '{printf "%0.4d%0.2d%0.2d\n",$3,$2,$1}'`
if [ `date -d $olddate '+%s'` -gt `date -d $newdate '+%s'` ];then
echo "olddate"
else
echo "newdate"
fi
When the dates are in YYYYMMDD format, a simple numeric comparison would do:
if [ "$olddate" -gt "$newdate" ]; then
Here's a script that will compare the dates by converting them to seconds since epoch first and them making the comparison:
#!/bin/sh
#
# define usage if we don't receive the correct
# number of arguments
if [ $# -ne 2 ]
then
echo "Usage: ${0##*/} <date_1> <date_2>"
exit 1
fi
# store initial date format
date1=$1
date2=$2
# convert the date format so that we can feed it
# to the date command by swapping the placement
# of the month and day
date_1=$(echo $1 | awk -F/ '{print $2"/"$1"/"$3}')
date_2=$(echo $2 | awk -F/ '{print $2"/"$1"/"$3}')
# convert the dates to seconds since epoch
date_1_seconds=$(date --date="$date_1" +%s)
date_2_seconds=$(date --date="$date_2" +%s)
# now compare the dates
if [ $date_1_seconds -gt $date_2_seconds ]
then
echo "$date1 is greater than $date2"
else
echo "$date2 is greater than $date1"
fi
# done
exit 0
Run it like so:
./compDate.sh
Usage: compDate.sh <date_1> <date_2>
./compDate.sh 25/04/2013 20/07/2012
25/04/2013 is greater than 20/07/2012