Operating System - HP-UX
1819681 Members
3665 Online
109605 Solutions
New Discussion

How to find yesterday's date in a shell script

 
Tom Chapel
Occasional Contributor

How to find yesterday's date in a shell script

I need to be able to manipulate dates in shell scripts, for example, how can I
determine yesterday's date without resorting to c or awk(systime()function n/a)
or perl(can't seem to get localtime() to work. Thanks for your help.
1 REPLY 1
Anthony Goonetilleke_1
Regular Advisor

Re: How to find yesterday's date in a shell script

here is a nifty little shell script function but I cannot take credit for it
have a look at

http://www.injunea.demon.co.uk/pages/page212.htm#15-1



#=====================================================================
s_back_date # (c) RHReepe. Returns a date string DAYS back
#=====================================================================
# Arg_1 = [DAYS:-1]
{
days=${1:-1}
date_d=`date +%d`
date_m=`date +%m`
date_y=`date +%Y`
#--------------------------------
# Days Back Size Test
#--------------------------------
if [ $days -lt $date_d ]
then
date_d=`expr $date_d - $days`
else
days=`expr $days - $date_d`
date_m=`expr $date_m - 1`
month_length=`s_month_length $date_m`
while [ $days -gt $month_length ]
do
days=`expr $days - month_length`
date_m=`expr $date_m - 1`
month_length=`s_month_length $date_m`
done
date_d=`expr $month_length - $days`
fi
#--------------------------------
# Date String Padding
#--------------------------------
if [ $date_d -lt 10 ]
then
date_d="0"$date_d
fi
if [ $date_m -lt 10 ]
then
date_m="0"$date_m
fi
echo $date_y $date_m $date_d
}