I can't comment on the accepted answer, so'll write it as a new one.
Attention, the accepted answer will produce an unexpected result (the wrong month) in some cases, like if you run the script on 31th.
To illustrate, run this two examples on your bash shell (if your date utility doesn't support the needed options, let me know which one you're using and i'll try to adapt it).
:: From accepted answer (for simulated day 2020-October-31)
echo "Prior month? : $(date '+%b-%Y' -d "$(date -d "2020-10-31") 1 month ago")"
Prior month? : Oct-2020
- "October", probably not what you want.
versus (a more reliable solution)
echo "Prior month? : $(date '+%b-%Y' -d "$(date +%Y-%m-1 -d "2020-10-31") 1 month ago")"
Prior month? : Sep-2020
- "September" the correct prior month.
This happens because the date utility, for 'relative' calculations, for month usually uses 30 days and not "a month".
To work around this, you'll can calculate the prior month based on day 1 (or 15) instead of using the current day.
Adapting @Milag script, it would look something like.
#!/usr/bin/env bash
lastmoyr=$(date '+%b-%Y' -d "$(date +%Y-%m-1) 1 month ago")
lm=$(date '+%-m' -d "$(date +%Y-%m-1) 1 month ago")
echo last month-year "$lastmoyr"
echo last month number "$lm"
# Mar Jun Sep Dec ==> 3 6 9 12
if [[ $((lm % 3)) == 0 ]]; then
runscript1 "$lastmoyr"
else
runscript2 "$lastmoyr"
fi