rakaposhi 0 Newbie Poster

Hello List,

I do not have any experiece with the shell scripting. I need the help to solve an issue.
This script is calculating the value of Nth day correctly which is just for next one month.
Now what I need to do is to calculate the same occurence pattern for next 12 months.

I will really really appreciate if someone can help me what to adjust in the code which will bring the value of Nth day for whole next 12 months.

#! /bin/ksh
# NthWeekday.sh - returns the date (day only) of the Nth Weekday of the month
# Such as First Monday, Second Wednesday, etc
# Usage: NthWeekday.sh Month Year Weekday Nth
# Month - The month to search, numeric format (ie 1, 2, 3, etc)
# Year - The year to search, four digits (ie 2008, 2009, etc)
# Weekday - The day of the week to search for (ie Sunday, Monday, etc)
# Nth - The ordinal value to search for (ie First, Second, Third, Fourth)


LOGFILE=/logs/NthWeekday.log
MONTH=$1
YEAR=$2
WEEKDAY=$3
NTH=$4

echo $$,`date`",Running NthWeekday.sh $MONTH $YEAR $WEEKDAY $NTH" >> $LOGFILE

# Convert WEEKDAY to numeric format
if [ "$WEEKDAY" = "Sunday" ]; then
WEEKDAY=0
fi
if [ "$WEEKDAY" = "Monday" ]; then
WEEKDAY=1
fi
if [ "$WEEKDAY" = "Tuesday" ]; then
WEEKDAY=2
fi
if [ "$WEEKDAY" = "Wednesday" ]; then
WEEKDAY=3
fi
if [ "$WEEKDAY" = "Thursday" ]; then
WEEKDAY=4
fi
if [ "$WEEKDAY" = "Friday" ]; then
WEEKDAY=5
fi
if [ "$WEEKDAY" = "Saturday" ]; then
WEEKDAY=6
fi

# Convert NTH to numeric format
if [ "$NTH" = "First" ]; then
NTH=1
fi
if [ "$NTH" = "Second" ]; then
NTH=2
fi
if [ "$NTH" = "Third" ]; then
NTH=3
fi
if [ "$NTH" = "Fourth" ]; then
NTH=4
fi

# Find the date for the first WEEKDAY in MONTH
FIRSTSATURDAY=`cal $MONTH $YEAR | sed -n '3s/. //gp'`
FIRSTWEEKDAY=`expr $FIRSTSATURDAY + 7 + $WEEKDAY`
FIRSTWEEKDAY=`expr $FIRSTWEEKDAY % 7 + 1`

# Find the date for the Nth WEEKDAY in MONTH
TEMP=`expr $NTH - 1`
NTHWEEKDAY=`expr $FIRSTWEEKDAY + 7 \* $TEMP`
echo $NTHWEEKDAY
echo $$,`date`",NthWeekday.sh returned $NTHWEEKDAY" >> $LOGFILE

#exit

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.