#!/bin/bash
while getopts :dls _inst
do case $_inst in
(d) format='%1d%.0s\n' ;; (l) format='%0.s%-s\n' ;; (s) format='%0.s%-.3s\n' ;; esac
done
shift $((OPTIND-1))
((!$: ${format:='%0.s%-.3s\n'}
: ${1:?missing month parameter [1-12]}
: ${2:?missing day parameter [1-31]}
cal $1 ${3:-$(date +%Y)} | gawk -FX '
BEGIN { day="Sunday Monday Tuesday WednesdayThursday Friday Saturday"
sub(/^0/, "", daynum)
dayre="(^| )" daynum "( |$)"
}
NR==1 || NR==2 \
{ next }
dayre { if (match($0, dayre))
{ if (daynum<=9 || RSTART==1) RSTART-=1
exit
}
}
END { printf format, RSTART/3, substr(day, RSTART*3+1, 9)
}
' daynum=$2 format=$format -
exit 0