Operating System - HP-UX
1752589 Members
4692 Online
108788 Solutions
New Discussion юеВ

Re: how to write a script to run last sunday in a month

 
RAC_1
Honored Contributor

Re: how to write a script to run last sunday in a month

Why go through this all???

cal | cut -c1-2|grep -v "^$"|tail -1 will give the date of the last sunday.

Now you have the date, set the script.

Anil
There is no substitute to HARDWORK
John Poff
Honored Contributor

Re: how to write a script to run last sunday in a month

Muthukumar,

Thanks for the praise. I like to keep things simple. I'm too lazy to do all that hard thinking! :)

You can modify your code just slightly and get the last day of the month for any weekday. Just move the 'grep' after the 'awk', so that the blank lines are removed after the dates are printed. For example, to test for the last Friday of the month, this would work:

if [[ $(date +'%e') -ne $(cal | awk '{ print $6}' | grep -v "^$" | tail -1) ]]

Then you just adjust the variable in the awk print statement for the day of the week you want ($1 through $7 for Sunday through Saturday).

JP


Geoff Wild
Honored Contributor

Re: how to write a script to run last sunday in a month

Anil - wow - using cal - that's cool - that's worth 11 points in my book :)

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
A. Clay Stephenson
Acclaimed Contributor

Re: how to write a script to run last sunday in a month

Here is one method. First create a cron entry to run your script every Sunday; theyt script itself will then decide whether or not to simply exit and do nothing or continue.

#!/usr/bin/sh

export PATH=${PATH}:/usr/local/bin

if [[ $(caljd.sh -M) -eq $(caljd.sh -n 7 -M) ]]
then
exit 0
else
echo "Last Sunday in month; do your thing"
fi

The idea is that that it looks at the current day's month and compare's it to that 7 days hence. If they differ, it's the last time that weekday occurs in the month.

By the way, caljd.sh -N will yield the occurence of a given weekday within the month.

Invoke as caljd.sh -u for full usage and examples.

If it ain't broke, I can fix that.
Sridhar Bhaskarla
Honored Contributor

Re: how to write a script to run last sunday in a month

Hi,

I was stunned by that one liner but that's a great try. For ex.,

cal 08 04 |tail -2 |head -1 |awk '{print $1}'

is not giving me the last sunday ;-).

I believe the best approach is given by Clay. We use similar logic here where some of our maintenance windows are on 2nd Sunday!!. Add 7 to the current date and if the resulting date is of next month, then that's it. Run it every sunday.

#!/usr/bin/ksh
NOW=$(/usr/contrib/bin/perl -e "printf("%d\n",time())")
MON=$(date +%b)


(( NEXTMON_SECS = $NOW + ( 7 * 86400 ) ))


NEXTMON=$(echo "0d${NEXTMON_SECS}=Y" |adb |awk '{print $2}' )

if [[ "$MON" != "$NEXTMON" ]]
then
echo "OK!.. this is the last sunday."
#your_function
else
echo "More sundays to go"
exit 0
fi

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Geoff Wild
Honored Contributor

Re: how to write a script to run last sunday in a month

# cal 08 04
August 4
S M Tu W Th F S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

# cal 08 04 | cut -c1-2|grep -v "^$"|tail -1
31

Looks like last Sunday to me...

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Geoff Wild
Honored Contributor

Re: how to write a script to run last sunday in a month

Of course cal 08 04 is 1904... :)

# cal 08 2004 | cut -c1-2|grep -v "^$"|tail -1
29

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
TDN
Advisor

Re: how to write a script to run last sunday in a month

Hi,

Thank you all of you.

I think I go for the solution to put the script in cron running every sunday and check out if the date is the last sunday inside the script.

I found a example on bigadmin.com

Once again, thanks guys.

Regards
TDN