You are not logged in.
Pages: 1
I've been trawling the interwebs looking for a script in pure shell that will subtract n days from today's date -- I was actually hoping someone had already written a direct equivalent of DATEMATH.
No such luck. So I hacked this together. It's far from perfect, but it fulfills my needs (for now). Maybe it'll prove useful to someone else.
NB - My target shell is a bastardisation of ZSH and cygwin with some custom features (called NSH). The "date" command it uses doesn't support any fancy business around converting time since epoch back and forth to readable dates -- that would have made life far too easy.
#!/bin/nsh
offset=${1:-"0"}
if [ $offset -lt 0 ] ; then echo "Not supported" >&2 ; return 1 ; fi
month_name=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
month_days=(31 XX 31 30 31 30 31 31 30 31 30 31)
off_day=$(date '+%d')
off_month=$(date '+%m')
off_year=$(date '+%Y')
let off_day-=$offset
while [ $off_day -le 0 ]
do
let off_month-=1
if [ $off_month = 0 ]
then
let off_month=12
let off_year-=1
fi
if [ $off_month = 2 ]
then
month_days[2]=28
if [[ $(( $off_year % 4 )) -eq 0 ]] ; then month_days[2]=29 ; fi
if [[ $(( $off_year % 100 )) -eq 0 ]] ; then month_days[2]=28 ; fi
if [[ $(( $off_year % 400 )) -eq 0 ]] ; then month_days[2]=29 ; fi
fi
let off_day+=${month_days[$off_month]}
done
off_day=0${off_day}
off_month=0${off_month}
if [[ $off_year -le 1752 ]] ; then echo "Out of range" >&2 ; return 2 ; fi
echo "${off_day[-2]}${off_day[-1]}/${off_month[-2]}${off_month[-1]}/${off_year}"
echo "${month_name[$off_month]} ${off_day[-2]}${off_day[-1]}"
return 0
cmd | *sh | ruby | chef
Offline
Pages: 1