Monday, June 18, 2007

get certain date/month on shell script ( 5 dayz in this script )

month=`date +%m`
day=`date +%d`
year=`date +%Y`
month=`expr $month + 0`
day=`expr $day - 5` # <- changeable
if [ $day -lt 1 ]; then
month=`expr $month - 1`
minusfix=`expr 0 - $day`
#echo $minusfix
if [ $month -eq 0 ]; then
month=12
day=31
year=`expr $year - 1`
else
case $month in
1|3|5|7|8|10|12) day=`expr 31 - $minusfix`;;
4|6|9|11) day=`expr 30 - $minusfix`;;
2)
if [ `expr $year % 4` -eq 0 ]; then
if [ `expr $year % 400` -eq 0 ]; then
day=29
elif [ `expr $year % 100` -eq 0 ]; then
day=28
else
day=29
fi
else
day=28
fi
;;
esac
fi
fi
case $month in
[0-9]) month="0$month";;
esac
case $day in
[0-9]) day="0$day";;
esac
date=$year$month$day
echo "tanggal sekarang `date +%Y%m%d`"
echo "5 hari yg lalu : $date"

1 Comment :

Anonymous said...

Good for people to know.