Sridhar Bhaskarla
Honored Contributor

Re: how to write a script to run last sunday in a month

Hi,

I was stunned by that one liner but that's a great try. For ex.,

cal 08 04 |tail -2 |head -1 |awk '{print $1}'

is not giving me the last sunday ;-).

I believe the best approach is given by Clay. We use similar logic here where some of our maintenance windows are on 2nd Sunday!!. Add 7 to the current date and if the resulting date is of next month, then that's it. Run it every sunday.

#!/usr/bin/ksh
NOW=$(/usr/contrib/bin/perl -e "printf("%d\n",time())")
MON=$(date +%b)


(( NEXTMON_SECS = $NOW + ( 7 * 86400 ) ))


NEXTMON=$(echo "0d${NEXTMON_SECS}=Y" |adb |awk '{print $2}' )

if [[ "$MON" != "$NEXTMON" ]]
then
echo "OK!.. this is the last sunday."
#your_function
else
echo "More sundays to go"
exit 0
fi

-Sri
You may be disappointed if you fail, but you are doomed if you don't try