To deal with dates in the shell, it is useful to convert to Julian Day values (
The listing below provides two functions for Julian Day conversions that I implemented in KornShell based on C routines appearing in
The C Users Journal.
Save the listing to a file, source it ( [tt]. <filename>[/tt] ), and then yesterday's date can be calculated like so:
[tt]
today=$(ToJul `date +"%m %e %Y"`)
(( yesterday = today -1 ))
set `FromJul $yesterday`
month=$1
day=$2
year=$3
echo "$month/$day/$year"
[/tt]
Hope this helps.
**begin script**
[tt]
#!/bin/ksh
# KornShell implementation of Julian Day functions
# ToJul and FromJul functions based on C routines presented by
# David Burki, The C Users Journal, Vol. 11 , No 2, February, 1993,
# page 30.
#
# Burki's algorithm was an adaptation of the FORTRAN code used to
# implement the algorithm presented by H. Fliegl and T. Van Flanders,
# Communications of the ACM, Vol. 11, No. 10, October, 1968, page 657.
function ToJul #month day year
{
# ToJul prints the Julian Day value for the date passed
integer lmonth=${1}
integer lday=${2}
integer lyear=${3}
# set to defaults if null
if [[ ${lmonth}"" -eq "" ]]
then
lmonth=$(date +"%m"

fi
if [[ ${lday}"" -eq "" ]]
then
lday=$(date +"%d"

fi
if [[ ${lyear}"" -eq "" ]]
then
lyear=$(date +"%Y"

fi
jul_day=$(( lday - 32075 + 1461 *
(lyear + 4800 + (lmonth - 14) /12) /
4 + 367 * (lmonth - 2 - (lmonth -14) /
12 * 12 ) / 12 - 3 * ((lyear + 4900 +
(lmonth - 14) / 12 ) / 100) /4 ))
echo ${jul_day}
}
function FromJul #julian_day
{
# FromJul prints the date represented by the passed Julian Day value in the
# form "month day year"
integer t1
integer t2
integer yr
integer mo
integer jul_date=${1}
integer month
integer day
integer year
t1=$((jul_date + 68569))
t2=$(( 4 * t1 / 146097 ))
t1=$(( t1 - (146097 * t2 + 3)/4))
yr=$(( 4000 * ( t1 + 1) /1461001 ))
t1=$(( t1 - 1461 * yr /4 + 31 ))
mo=$(( 80 * t1 / 2447 ))
day=$(( t1 - 2447 * mo / 80 ))
t1=$(( mo / 11 ))
month=$(( mo + 2 - 12 * t1 ))
year=$(( 100 * (t2 - 49) + yr + t1 ))
echo "${month} ${day} ${year}"
}
function DayOfWeek #julian_day
{
integer julian_day=${1}
echo $(( julian_day % 7 ))
}
[/tt]
**end script**
Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L