Quick Help in shell script

Need to subtract date stored in variable from the current date, answer should come in days..and months.

Suppose two variable having value like

A=�Wed Jan 15 08:59:08 GMT 2014�
B= `date`      #Sun May 23 09:29:40 GMT 2010
SubtractAB= $A-$B #..?
AddAB=$A+$B #... ?
C=$B+9 # Sun May 23 09:29:40 GMT 2010 +9 = ??

Anyone who can solve it?

Thanks,
kuldeep

Have a read of one of these links:

http://www.unix.com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html

Yes Franklin,

I went through the post. it is really good one but only datecalc is fulfilling my requirement.
I have tried it by coping to my local machine.
BUT i am working on Qualified environment and can not create such huge function/script.
I tried to extract the date functionality but i couldn't work.

Let me explain you about my requirement.

this is my script

#!/usr/bin/ksh
HTML=/export/home/some/report.html
TEMP_FILE=/export/home/some/report.tmp
My_File=/export/home/some/My_File.txt
rm -rf $HTML $TEMP_FILE
touch $HTML $TEMP_FILE
SUBJECT="Truststore Validity Report on `date`"
MAIL_LIST="kuldeepsingh.yadav@ge.com"
#obtain current system date
YEAR=`date '+%Y'`
#let "YEAR= $YEAR+8"
MONTH=`date '+%m'`
let "tmpMonth=$MONTH+3"
let "lastYear=$YEAR-1"

if [ $tmpMonth -gt 12 ] ; then
let "newMonth=$tmpMonth-12"
let "newYear=$YEAR+1"
else
let "newMonth=$tmpMonth"
let "newYear=$YEAR"
fi

MONTHS="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
set -A MON $MONTHS
echo "tempMonth:$tmpMonth tempYear:$YEAR newMonth:$newMonth newYear:$newYear"
thirdMonth=${MON[$newMonth-1]}
#if [ newYear = "$YEAR"]
secondMonth=${MON[$newMonth-2]}
firstMonth=${MON[$newMonth-3]}
expiredMonth=${MON[$newMonth-4]}
#echo "thirdMonth:$thirdMonth secondMonth:$secondMonth firstMonth:$firstMonth expiredMonth:$expiredMonth"

# Function to append output (in html format) to file.
build_html() {
        echo $1 >> $HTML
        echo $1
}

################################################################################

cat $My_File | egrep "Alias|Valid" > $TEMP_FILE
      
{
 while read rec; do
         getLine=`echo $rec | awk '{print $1;}'`
 if [ "${getLine}" = "Valid" ] ; then
line2=`echo $rec|cut -c13-76`
getYear=`echo $rec|cut -c73-76`
getMonth=`echo $rec|cut -c53-55`
#echo "getYear-getMonth: $getYear-$getMonth"

if [ $thirdMonth = "$getMonth" -a $newYear = "$getYear" ] ; then
         build_html "<td bgcolor=FFFF00>${line2}</td>"
         build_html "</tr>"
elif [ $secondMonth = "$getMonth" -a $newYear = "$getYear" ] ; then
         build_html "<td bgcolor=FFCC00>${line2}</td>"
         build_html "</tr>"
elif [ $firstMonth = "$getMonth" -a $newYear = "$getYear" ] ; then
         build_html "<td bgcolor=FF6600>${line2}</td>"
         build_html "</tr>"
elif [ $expiredMonth = "$getMonth" -a $newYear = "$getYear" -o $lastYear = "$getYear" ] ; then
         build_html "<td bgcolor=999999>${line2}</td>"
         build_html "</tr>"
else
        build_html "<td>${line2}</td>"
        build_html "</tr>"
  fi
fi
        done } < ${TEMP_FILE}

build_html "</code>"
build_html "</tr>"
build_html "</table>"
build_html "</tr>"
build_html "</table>"
done

cat $HTML

Input file ($TEMP_FILE.)

Input file which is created by keytool and stored in $TEMP_FILE.

 Tue Mar 11 05:17:41 GMT 2008 until: Fri Mar 09 05:17:41 GMT 2018
 Thu Jun 12 18:36:48 GMT 2008 until: Sun Jun 10 18:36:48 GMT 2018
 Thu Jun 12 18:29:42 GMT 2008 until: Sun Jun 10 18:29:42 GMT 2018
 Tue Mar 11 05:18:02 GMT 2008 until: Fri Mar 09 05:18:02 GMT 2018
 Tue May 27 16:29:27 GMT 2008 until: Fri May 25 16:29:27 GMT 2018
 Tue May 27 16:40:04 GMT 2008 until: Fri May 25 16:40:04 GMT 2018
 Tue Mar 11 05:18:02 GMT 2008 until: Fri Mar 09 05:18:02 GMT 2018
 Tue May 27 16:29:27 GMT 2008 until: Fri May 25 16:29:27 GMT 2018
 Tue May 27 16:40:04 GMT 2008 until: Fri May 25 16:40:04 GMT 2018

Can you help me to highlight those lines from input files which are having "until:somedate" are older then today in red and those about to come in 3 months time as orange.
the time stamp will be taken from input file after the until.

I am really wondering on this forum for getting this done. today it is almost 3 days.

Something like this maybe ?

$ 
$ 
$ cat f9
Tue Mar 11 05:17:41 GMT 2008 until: Fri Mar 09 05:17:41 GMT 2010
Thu Jun 12 18:36:48 GMT 2008 until: Sun Jun 10 18:36:48 GMT 2010
Thu Jun 12 18:29:42 GMT 2008 until: Sun Jun 10 18:29:42 GMT 2010
Tue Mar 11 05:18:02 GMT 2008 until: Fri Mar 09 05:18:02 GMT 2010
Tue May 27 16:29:27 GMT 2008 until: Fri May 25 16:29:27 GMT 2010
Tue May 27 16:40:04 GMT 2008 until: Fri May 25 16:40:04 GMT 2010
Tue Mar 11 05:18:02 GMT 2008 until: Fri Mar 09 05:18:02 GMT 2010
Tue May 27 16:29:27 GMT 2008 until: Fri May 25 16:29:27 GMT 2010
Tue May 27 16:40:04 GMT 2008 until: Fri May 25 16:40:04 GMT 2010
Tue May 27 16:40:04 GMT 2008 until: Sun Aug 22 16:40:04 GMT 2010
Tue May 27 16:40:04 GMT 2008 until: Mon Aug 23 16:40:04 GMT 2010
Tue May 27 16:40:04 GMT 2008 until: Tue Aug 24 16:40:04 GMT 2010
Tue May 27 16:40:04 GMT 2008 until: Sat Dec 25 16:40:04 GMT 2010
$ 
$ 
$ cat -n f9.pl
     1    #!/usr/bin/perl -w
     2    use Date::Calc qw(Parse_Date Today Delta_Days Add_Delta_YMD Date_to_Days);
     3    $file = "f9";
     4    @y = Today();
     5    @z = Add_Delta_YMD(@y, 0,3,0);
     6    open (IN, $file) or die "Can't open $file: $!";
     7    while (<IN>) {
     8      chomp;
     9      /^.*?until: (.*?)$/ and $testdate = $1;
    10      @x = Parse_Date ($testdate);
    11      $dd = Delta_Days(@y, @x);
    12      print $_,"\t=>\t";
    13      if ($dd < 0) {
    14        print "Earlier than today";
    15      } elsif (Delta_Days(@x, @z) >= 0) {
    16        print "Between today and the next 3 months";
    17      } else {
    18        print "Later than the next 3 months";
    19      }
    20      print "\n";
    21    }
    22    close (IN) or die "Can't close $file: $!";
    23    
$ 
$ 
$ perl f9.pl
Tue Mar 11 05:17:41 GMT 2008 until: Fri Mar 09 05:17:41 GMT 2010    =>    Earlier than today
Thu Jun 12 18:36:48 GMT 2008 until: Sun Jun 10 18:36:48 GMT 2010    =>    Between today and the next 3 months
Thu Jun 12 18:29:42 GMT 2008 until: Sun Jun 10 18:29:42 GMT 2010    =>    Between today and the next 3 months
Tue Mar 11 05:18:02 GMT 2008 until: Fri Mar 09 05:18:02 GMT 2010    =>    Earlier than today
Tue May 27 16:29:27 GMT 2008 until: Fri May 25 16:29:27 GMT 2010    =>    Between today and the next 3 months
Tue May 27 16:40:04 GMT 2008 until: Fri May 25 16:40:04 GMT 2010    =>    Between today and the next 3 months
Tue Mar 11 05:18:02 GMT 2008 until: Fri Mar 09 05:18:02 GMT 2010    =>    Earlier than today
Tue May 27 16:29:27 GMT 2008 until: Fri May 25 16:29:27 GMT 2010    =>    Between today and the next 3 months
Tue May 27 16:40:04 GMT 2008 until: Fri May 25 16:40:04 GMT 2010    =>    Between today and the next 3 months
Tue May 27 16:40:04 GMT 2008 until: Sun Aug 22 16:40:04 GMT 2010    =>    Between today and the next 3 months
Tue May 27 16:40:04 GMT 2008 until: Mon Aug 23 16:40:04 GMT 2010    =>    Between today and the next 3 months
Tue May 27 16:40:04 GMT 2008 until: Tue Aug 24 16:40:04 GMT 2010    =>    Later than the next 3 months
Tue May 27 16:40:04 GMT 2008 until: Sat Dec 25 16:40:04 GMT 2010    =>    Later than the next 3 months
$ 
$ 

tyler_durden