转自酷勤网,自己略有改动。
#!/usr/bin/ksh
nowDate=`date + %Y%m%d`
echo "今天日期:" $nowDate
#返回月份的天数
get_mon_days()
{
Y=`expr substr $1 1 4`
M=`expr substr $1 5 2`
r1=`expr $Y \% 4`
r2=`expr $Y \% 100`
r3=`expr $Y \% 400`
case $M in
01|03|05|07|08|10|12) days=31;;
04|06|09|11) days=30;;
esac
if [ $M -eq 02 ]
then
if [ r1 -eq 0 -a r2 -ne 0 -o r3 -eq 0 ]
then
days=29
else
days=28
fi
fi
echo $days
}
#返回昨天日期
get_last_date()
{
Y=`expr substr $1 1 4`
M=`expr substr $1 5 2`
D=`expr substr $1 7 2`
YY=`expr $Y - 1`
MM=`expr $M - 1`
DD=`expr $D - 1`
MM=`printf "%02d" $MM`
DD=`printf "%02d" $DD`
lastMonth=$Y$MM
lastMonthDays=`get_mon_days $lastMonth`
last_date=$Y$M$DD
if [ $D -eq 01 ]
then
if [ $M -ne 01 ]
then
last_date=$Y$MM$lastMonthDays
fi
if [ $M -eq 01 ]
then
last_date=$YY"1231"
fi
fi
echo $last_date
}
lastDate=`get_last_date $nowDate`
echo "昨天日期:" $lastDate
#返回明天日期
get_next_date()
{
Y=$(expr substr $1 1 4)
M=$(expr substr $1 5 2)
D=$(expr substr $1 7 2)
YY=$(expr $Y + 1)
MM=$(expr $M + 1)
DD=$(expr $D + 1)
MM=$(printf "%02d" $MM)
DD=$(printf "%02d" $DD)
r1=$(expr $Y \% 4)
r2=$(expr $Y \% 100)
r3=$(expr $Y \% 400)
next_date=$Y$M$DD
if [ $D -eq 30 ]
then
case $M in
04|06|09|11) next_date=$Y$MM"01";;
esac
fi
if [ $D -eq 31 ]
then
next_date=$Y$MM"01"
case $M in
12) next_date=$YY"0101";;
esac
fi
if [ $M -eq 02 ]
then
if [ r1 -eq 0 -a r2 -ne 0 -o r3 -eq 0 ]
then
if [ $D -eq 29 ]
then
next_date=$Y$MM"01"
fi
else
if [ $D -eq 28 ]
then
next_date=$Y$MM"01"
fi
fi
fi
echo $next_date
}
nextDate=`get_next_date $nowDate`
echo "明天日期:" $nextDate