Categories
Unix Notes

Date entertainment

get last month using bash and the date command

MON=`date +”%m”`
((MON=$MON-1))
-bash: ((: MON=09: value too great for base (error token is “09”)

So, do this

MON=`date +”10#%m”`
((MON=$MON-1))
echo $MON
8

How entertaining – bash treats the leading zero as an indicator of octal.

Same would need to be done for any of the `date` formats that return leading zero as part of the result.

to get the seconds since EPOCH for other reasons –

date -r $((`date ‘+%s’` – 86400))

This version has no problem with the boundary condition of month number 1

MYDATE=`date +”%Y%m”`
YEAR=${MYDATE:0:4}
MONTH=$((10#${MYDATE:4:2} – 1))
[[ $MONTH == 0 ]] && MONTH=12 && ((YEAR–))

theReport=Web-Report-Monthly-$YEAR-$MONTH.html