using cal terminal command for next and previous months
i use the terminal command:
cal -m $(date %mm)
to get a calender of the CURRENT month.
but what string does one use for the PREVIOUS or NEXT month?
thanks!
n/a, Mac OS X (10.5.6)
n/a, Mac OS X (10.5.6)
#!/bin/bash
year=$(date +%Y)
month=$(date +%m)
prev=$((month-1))
next=$((month+1))
#display previous month
if [ $prev -eq 0 ] ;
then
cal 12 $((year-1))
else
cal $prev $year
fi
#display current month
cal $month $year
#display next month
if [ $next -eq 13 ] ;
then
cal 1 $((year+1))
else
cal $next $year
fi
cal $(date -v -1m "+%m %Y") # Last month
cal $(date "+%m %Y") # This month
cal $(date -v +1m "+%m %Y") # Next month
man cal
man date
One more thought, is there a good way to do this such that you don't call date more than once. Running this near/at midnight at the end of the month could cause a month to be skipped. (This is more an exercise than answering the original question)
#!/bin/sh
D=( $(date "+%m %Y") )
M=${D[0]}
Y=${D[1]}
cal $((M-1 < 1 ? 12 : M-1 )) $((M-1 < 1 ? Y-1 : Y))
cal $M $Y
cal $((M+1 > 12 ? 1 : M+1 )) $((M+1 > 12 ? Y+1 : Y))
using cal terminal command for next and previous months