Nthday

From SmlugWiki

Revision as of 03:16, 20 March 2007 by 209.16.66.222 (Talk)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

After deciding to write a cron job that emailed SMLUG's email list about the next meeting, I found cron's limitation in scheduling. If you want to have a cron job on the 3rd Monday of every month, it doesn't work well. I found this script on the linux elitist lists, but it had a bug. The script doesn't tell if it is the 3rd Monday, just if Monday is in the 3rd week.

Here is the original from the elitist mailing list:

#!/bin/bash
# Karsten M. Self Mon Mar 21 16:57:30 PST 2005
# Free use, distribution, and modification with no restrictions, granted.
# NO WARRANTY
# -------------------------------------------------
# Test if today is the nth weekday of the month.
# -------------------------------------------------
#
export PATH=/usr/bin:/bin
#
NTHDAY=$1 
#
if [ $(( ( ( $( date +%-d) + 1 ) /7 ) )) -eq $NTHDAY ]
    then true
    else false
fi

Here is my corrected version:

#!/bin/bash
# Karsten M. Self Mon Mar 21 16:57:30 PST 2005
# Free use, distribution, and modification with no restrictions, granted.
# NO WARRANTY
# -------------------------------------------------
# Test if today is the nth weekday of the month.
# -------------------------------------------------
#
export PATH=/usr/bin:/bin
#
NTHDAY=$1
MONTH=$( date +%m)
YEAR=$( date +%Y)
DAYSTART=`date -d "$MONTH/1/$YEAR" +%w`
FWD=$2
if [ $(( ( ( ( ( $( date +%-d) + 1 ) + $DAYSTART ) + $FWD ) /7 ) )) -eq $NTHDAY ]
    then
      true
    else false
fi

This version takes an additional argument on the command line. Since I want to send my email on the Friday before the 3rd Monday, I have to check 4 days in advance.

The format is:

nthday.sh <week Number> <day offset>

So following that format, here is how to check for the 3rd monday being 4 days ahead of now, and mail my lug if it is. The percent signs are automatically changed to newlines in a cron job, so don't get confused by them.

nthday.sh 3 4 && mail -s "Meeting Reminder" all@smlug.com,%Reminder%%Meeting 7pm Monday%
Personal tools