[wplug] Determine the "Last Full Week" of the month

Eric Cooper ecc at cmu.edu
Wed Oct 20 10:08:53 EDT 2010


On Wed, Oct 20, 2010 at 09:37:10AM -0400, Kevin Squire wrote:
> I have to work out a script (prefer bash in this case, but any will
> work) for work that will run on the 1 day of the week (sunday) but ONLY
> if it is the last full week of the month.

I think a simpler test is:

today is Sunday AND next Saturday (today + 6) is still in the current month
AND the Saturday after that (today + 13) is no longer in the current month

Here's a script using the wonderful date parsing of the "date" command:

#!/bin/sh

if [ $# -ne 1 ]; then
    echo Usage: $0 date
    exit 1
fi

day="$1"

if [ $(date --date "$day" +%A) != Sunday ]; then
    echo $day is not a Sunday
    exit 1
fi

month=$(date --date "$day" +%B)

if [ $(date --date "$day + 6 days" +%B) != $month ]; then
    echo $day is not in a full week
    exit 1
fi

if [ $(date --date "$day + 13 days" +%B) = $month ]; then
    echo $day is not in the last week
    exit 1
fi

echo $day is the Sunday of the last full week of $month
exit 0

-- 
Eric Cooper             e c c @ c m u . e d u


More information about the wplug mailing list