Tuesday, September 8, 2009

how to display date yesterday at unix

if u want to display date yesterday at unix with format YYYYMMDD, you can try with my script below :

if u type at unix shell with syntac date, so the result is below :
bash-3.2$ date
Tue Aug 18 08:41:44 wib 2009

we can change the format as our need. example i need format yyyymmdd.
please make script below and save to tanggal_hari_ini.sh

run as below

sh tanggal_hari_ini.sh

Input Script :

#!/usr/bin/bash
# bof date
month=`date +%m`
day=`date +%d`
year=`date +%Y`
month=`expr $month + 0`
day=`expr $day - 1`
if [ $day -eq 0 ]; then
month=`expr $month - 1`
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=31;;
4|6|9|11) day=30;;
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
yesterday=$year$month$day
echo $yesterday
# eof date
exit 0


output script : 20090818

0 comments: