The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-04-2008
rakaposhi rakaposhi is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 1
Pleasae Help!!!

Hi List,

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



Thanks in advace,
  #2 (permalink)  
Old 08-04-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Just put a loop around it:


Code:
mon=1
while ((mon<=12))
do
    ./NthWeekDay.sh $mon 2008 Sunday First
    ((mon=mon+1))
done

That would give you all of the first Sundays of the month for the year 2008.
Closed Thread

Bookmarks

Tags
date

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0