Date comparison

Hi all,

I've a shell script which runs on Bourne shell. I've to do a date comparison. If the value of the supplied parameter(in format YYYYMMDD)is greater than todays's date(format YYYYMMDD), it should prompt the user that the supplied date is greater than today's date. The script is given below.

#! /bin/sh
dateval=`date +%Y%m%d`
if [ $# -eq 0 ]
then
dateval=`date +%Y%m%d`
elif [ $1 -gt $dateval ];then
echo "Supplied date is greater than current date"
else
 dateval=$1
fi

The script gives the desired result.But, is there any other alternative to using 'gt' other than DateCalc function.

Any help will be appreciated.
Thanks,
Sumesh

If you have perl you can use this

cal_diff
###############################
#!/bin/perl
use Time::Local;
my $year=shift;
my $month=shift;
my $day=shift;
my $time = timelocal(0,0,23,$day,$month-1,$year-1900);
($DAY, $MONTH, $YEAR) = (localtime)[3,4,5];
my $now = timelocal(0,0,23,$DAY, $MONTH, $YEAR);
my $diff = $now - $time;
print "$diff\n";
###############################
Usage :
cal_diff yyyy mm dd

#! /bin/sh -vx
if [ $# -eq 0 ]
then
dateval=`date +%Y%m%d`
elif [ $(cal_diff $1 $2 $3) -lt 0 ];then
echo "Supplied date is greater than current date"
else
 dateval=$1$2$3
fi

Thanks for the reply.

I don't have perl. Is there any alternative in UNIX?

Thanks,
Sumesh

Why cant you use -gt ?

Try replacing

elif [ $1 -gt $dateval ];then

with

elif [ $(($1 - $dateval)) > 0 ]; then

Vino,
Thanks for the reply.

I tried running the following code

    elif [ $(($1 - $dateval)) > 0 ];then
       echo "Supplied date is greater than current date
 

but, the following error is displayed.

./shellpgm33.sh: syntax error at line 13: `(' unexpected

Why does this happen?.

Thanks in advance,
Sumesh

Well, what is line 13 ? Can you post the complete script ? Also post the output of uname -a

Here is the complete script.

#! /bin/sh

dateval=`date +%Y%m%d`
if [ $# -eq 0 ]
then
dateval=`date +%Y%m%d`
elif [ $1 -gt $dateval ];then
echo "Supplied date is greater than current date"
else
 dateval=$1
fi
echo $dateval
echo $(($1 - $dateval))

I did

uname -a

and the result is

SunOS secmstqatmp 5.8 Generic_117350-33 sun4u sparc SUNW,UltraAX-i2

Thanks in advance,
Sumesh