Computing.Net > Forums > Unix > determine cal

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

determine cal

Reply to Message Icon

Name: salem
Date: January 25, 2007 at 18:18:23 Pacific
OS: AIX
CPU/Ram: 400Mhz/2GB
Product: p560
Comment:

How can I setup a variable to get the second Tuesday of each month?



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: January 25, 2007 at 21:52:26 Pacific
Reply:

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 multpr

if [ $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=10

cnt=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"


0

Response Number 2
Name: thepubba1
Date: February 1, 2007 at 10:09:57 Pacific
Reply:

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


0

Response Number 3
Name: thepubba1
Date: February 1, 2007 at 10:32:46 Pacific
Reply:

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


0

Response Number 4
Name: nails
Date: February 3, 2007 at 07:53:14 Pacific
Reply:

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.


0

Response Number 5
Name: thepubba1
Date: February 4, 2007 at 08:31:48 Pacific
Reply:

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.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: determine cal

Script using cal www.computing.net/answers/unix/script-using-cal/7188.html

Determining Hardisk vendor www.computing.net/answers/unix/determining-hardisk-vendor/1749.html

Find the day of week using AWK www.computing.net/answers/unix/find-the-day-of-week-using-awk/5243.html