Listing 2: weekdate script
#!/bin/sh
#
# weekdate
#
# Outputs the day of the current month
# corresponding with the arguments of the form
# "2 Sun" where 2 is the 2nd and Sun is Sunday.
# The first argument may be any number from 1
# to 5 and the second may be any three-letter
# day of the week or a number from 0 to 6 (Sun
# to Sat).
#
# For use with cron, it is better to output a
# status if the current day is the specified
# weekdate of the month. Using this method
# allows commands such as
#
# weekdate 2 Tue && dosomething
#
# to be put into a crontab line executed
# sometime every Tuesday, or
#
# weekdate 4 Thu || dosomething
#
# to execute something every Thursday except
# the 4th one.
#
# Copyright, Jan 12, 1993 by Lawrence S Reznick
usage () {
echo $0: $1 \\n
echo usage: $0 whichone whichday \\n
echo where whichone is a number 1-5
echo and whichday is a day of the week name or number:
echo sun=0, mon=1, tue=2, wed=3, thu=4, fri=5, sat=6 \\n
echo Give day number corresponding with the requested weekdate,
echo such as second Tuesday by \"weekdate 2 tue\".
}
#
# Extract the day's column from the calendar.
# The extra date command after cal makes SCO compatible with SVR4.
# If using SVR4, it can be taken out.
#
calprog () {
cal `date '+%m %Y'`
}
daycolumn () {
calprog |
tail +3 |
cut -c$1
}
#
# Check arguments
#
if [ $# -lt 2 ]
then
usage "Missing arguments"
exit 1
fi
whichone=$1
whichday=$2
if [ $whichone -lt 1 -o $whichone -gt 5 ]
then
usage "First parameter out of range"
exit 1
fi
#
# Allow day of week to be number or case independent word abbreviation
#
sun=0; mon=1; tue=2; wed=3; thu=4; fri=5; sat=6
whichday=`echo $whichday | tr '[A-Z]' '[a-z]'`
case $whichday in
0 | 1 | 2 | 3 | 4 | 5 | 6 )
;;
sun | mon | tue | wed | thu | fri | sat )
whichday=`eval echo $"$whichday"`
;;
* )
usage "Second parameter \"$whichday\" out of range"
exit 1
;;
esac
#
# Set a field range of characters for cutting the day number out of cal
#
field=`expr $whichday \* 3`
field=`expr 1 + $field`-`expr 2 + $field`
if [ $whichone -eq 5 -a `daycolumn $field | wc -w` -lt 5 ]
then
echo "$0: Second parameter is a 5th day not in this month \n"
echo "Command was \"$0 $1 $2\" for the following month: \n"
calprog
exit 2
fi
#
# Figure out which day of the week was the first day of the month
#
firstday=`calprog | tail +3 | head -1 | wc -w`
firstday=`expr 7 - $firstday`
#
# If the day requested comes before the first day, start search in 2nd week
#
if [ $whichday -lt $firstday ]
then
whichone=`expr $whichone + 1`
fi
#
# Figure out which day of the month is the day requested
#
day=`daycolumn $field | tail +$whichone | head -1`
# Set the exit status according to whether today is that day of the month
# If you'd rather output the day number, uncomment the next line and
# comment the test line.
#echo $day
#test $day -eq `date '+%d'`
|