#!/bin/sh # whatyear weekday month day # given a month, day, and day of week figure out the most recent year it occurred temp=".$0.tmp" figureDOW() { # give this day of month, month and year and it'll return day of week, 1-7 day=$1; caldate="$2 $3" expr="[^0-9]${day}[^0-9]" cal $caldate | sed 's/^/ /;s/$/ /' > $temp NRval=$(cat $temp | awk "/$expr/ { print NR }") NFval=$(cat $temp | awk "/$expr/ { print NF }") if [ $NRval -eq 3 ] ; then DOW="$(( $day + ( 7 - $NFval ) ))" else DOW=$(cat $temp | awk "/$expr/ { for (i=1;i<=NF;i++) { if (\$i~/${day}/) { print i }}}") fi /bin/rm -f $temp } if [ $# -ne 3 ] ; then echo "Usage: $(basename $0) weekday month day" echo " (example: $(basename $0) wed aug 3 )" exit 1 fi weekday=$(echo $1 | tr '[[:upper:]]' '[[:lower:]]' | cut -c1-3) month=$(echo $2 | tr '[[:upper:]]' '[[:lower:]]' | cut -c1-3) day=$3 # convert weekday = DOW numeric value desiredDOW=$(echo $weekday | sed 's/sun/1/;s/mon/2/;s/tue/3/;s/wed/4/;s/thu/5/;s/fri/6/;s/sat/7/') # get the current month, day and year # echo $(date "+thismonth=%m; thisday=%d; thisyear=%Y") eval $(date "+thismonth=%m; thisday=%d; thisyear=%Y") # now convert month name into abbrev, then figure numerical equivalent monthnum=$(echo $month | sed 's/jan/1/;s/feb/2/;s/mar/3/;s/apr/4/;s/may/5/;s/jun/6/' | \ sed 's/jul/7/;s/aug/8/;s/sep/9/;s/oct/10/;s/nov/11/;s/dec/12/') # now, is the mon/day possibly this year? Only if it's in the past... if [ $monthnum -gt $thismonth ] ; then # month is in the future, can't be this year mostrecent=$(( $thisyear - 1 )) elif [ $monthnum -eq $thismonth -a $day -gt $thisday ] ; then # right month, but seeking a date in the future mostrecent=$(( $thisyear - 1 )) else mostrecent=$thisyear fi # echo Looking for $weekday, $day, $month \($monthnum\) starting in $mostrecent # now we need to loop through years, decrementing, until a match year=$mostrecent DOW=-1 # start with a dead value while [ $DOW -ne $desiredDOW ] do figureDOW $day $monthnum $year # echo "> $day $month occurred on a $DOW in $year" year=$(( $year - 1 )) done echo "Got it! $day $month occurred on a $weekday most recently in ${year}:" cal $month $year exit 0