Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
The GNU Linux tools such as gawk can probably do this, but using traditional unix tools, I've implemented a day-of-the-week algorithm in a shell script:
#!/bin/ksh
# This function returns the day of week where
# 0 = sunday, 1 = monday, ... 6 = saturday
# This algorithm is from Mike Keith's World of Words & Numbers:
# http://users.aol.com/s6sj7gt/mikeca...
# arguments: $1 = month, $2 = day, $3 = year in format YYYY
get_dow()
{
# [.] means to truncate downward to the nearest integer
# dayofweek=([23m/9] + d + 4 + y + [z/4] - [z/100] + [z/400] - 2 (if m >= 3)) mod 7
typeset -i DOW
typeset -i z
typeset -i multprif [ $1 -lt 3 ]
then # year determination
z=$(($3-1))
else
z=$3
fi
if [ $1 -ge 3 ]
then # set the multiplier
multpr=2
else
multpr=0
fi
DOW=$(( ((23 * $1/9) + $2 + 4 + $3 + ($z/4) - ($z/100) + ($z/400) - $multpr) % 7))echo $DOW
}# return a day string
# sunday, monday, .. saturday
get_day_string()
{
case $1 in
0) echo "sunday";;
1) echo "monday";;
2) echo "tuesday";;
3) echo "wednesday";;
4) echo "thursday";;
5) echo "friday";;
6) echo "saturday";;
*) echo "error";;
esac
}# Oct, 2006
YEAR=2006
MM=10cnt=0
DD=1
# loop until the second tuesday is reached
while true
do
day_str=$(get_day_string $(get_dow MM DD YEAR) )
if [[ $day_str = "tuesday" ]]
then
((cnt+=1))
if [[ $cnt -eq 2 ]]
then
break
fi
fi
((DD+=1))
done# day that's the 2nd tuesday
echo "Month: $MM Year: $YEAR"
echo "2nd Tuesday: $DD"

Hey Nails, I'm back. Couldn't remember my old password so I had to create another account. Anyway, I thought this would work.
#!/bin/ksh
counter=1
cal | while read line
do
if [[ ${counter} -eq 3 && ${#line} -lt 13 ]]
then
second=5
elif [[ ${counter} -eq 3 && ${#line} -ge 13 ]]
then
second=4
fi
if [[ $counter -eq $second ]]
then
print $line | while read Sun Mon Tue Rest
do
print "The second Tuesday of the month is the ${Tue}th"
done
exit
fi(( counter += 1 ))
done

Salem:
In regard to my previous example, it would be fairly easy to modity the script to take arguments such as month (default to current year) or month year.
Jerry Lemieux

Hey, Jerry, How are you? Nice to see you back.
I tried your script and it works - no surprise there. The algorithm doesn't jump out at me. Can you explain it? Thanks.

Nail:
The third line of the cal command will always return the string for the first week of the month. If the length of the third line is greater than or equal to 13, then that means that the first Tuesday occurs during the first week of the month so I grab the value of the third field in the 4th line. If it is less than 13, I know the 3rd field in the 5th line is the second Tuesday. I wrote this one back in 1999 when I had a requirement to run a statistical program every 3rd Saturday. I was using a self rescheduling 'at' job, but then came up with this idea and simply put the script in the cron to run every Saturday.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |