Run script on every last sunday of the month


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Run script on every last sunday of the month
# 1  
Old 07-21-2016
Wrench Run script on every last sunday of the month

Hi ALL,

I have been testing this script to run for every last Sunday of the month,looks like month which have 5 sunday (july 2016 )is not passing this and failing every time.

Here is what I am using,

Code:
current_date=31
        echo " CURRENT DAY -> $current_date"

        if [ $current_date -le 7 ]
        then
                week=A
        else
                if [ $current_date -gt 7 ] && [ $current_date -le 14 ];
                then
                        week=B
                else
                        if [ $current_date -gt 14 ] && [ $current_date -le 21 ];
                        then
                                week=C
                        else
                                if [ $current_date -gt 21 ] && [ $current_date -le 28 ] ;
                                then
                                        week=D
                                else
                                        week=E
                                fi
                        fi
                fi
        fi
        echo "CURRENT WEEK --> $week"
        echo ""

WEEK is a variable which I am passing with the script.
Code:
ksh test_script.sh D

Any clue will be highly appreciated.Thanks again for your time.
# 2  
Old 07-21-2016
Code:
ls=$(cal | awk '$1+=0 {ls=$1} END {print ls}')
ts=$(date +"%d")

[[ $ls = $ts ]] && echo "today is the last sunday of the month"

These 2 Users Gave Thanks to rdrtx1 For This Post:
# 3  
Old 07-21-2016
Hi netdbaind,
The problem with your logic is that the last Sunday in a month is 22-28 in a month with 28 days, 23-29 in a month with 29 days, 24-30 in a month with 30 days and 25-31 in a month with 31 days. There is nothing that you have shown us in your code that determines whether or not you are in a leap year nor what month you're evaluating.

The code rdrtx1 suggested works in any locale where cal will print Sundays as the leftmost field on a line. You could guarantee that the output from cal will give you what you want no matter where in the world you run it if you change the first line to:
Code:
ls=$(LC_ALL=C cal | awk '$1+=0 {ls=$1} END {print ls}')

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

cron job on 3rd sunday of every month

Hi, Actually scheduled my test scripts on test severs as shown below. They are supposed to run on 3rd sunday of every month. Unfortunately it ran on 2nd sunday of the month (suspecting that it will run every sunday). I am sorry if I miss something. Could you please let me know if I did any... (1 Reply)
Discussion started by: System Admin 77
1 Replies

2. Shell Programming and Scripting

Script to run on 2 4 and 7 day of the month

I am looking for a unix script which could run a job on 2, 4 and 7 working day of the month. if the days are falling on the saturday/sunday. it should run on the next day. Thank you.. (9 Replies)
Discussion started by: tradingspecial
9 Replies

3. Shell Programming and Scripting

Trigger the script if it is first sunday of the quarterly month

Hello All, I have script which needs to be scheduled in crontab which needs to be run only on the first sunday of the quarterly months (March, June, September,Dec). Can anyone please help me out on this. Thanks, Arun. (13 Replies)
Discussion started by: Arun1992
13 Replies

4. Shell Programming and Scripting

How to setup cronjob 3rd sunday of every month?

Hi All, I need to set up cronjob for every third sunday of the month. here i have seen one example for 4th sunday for every month in another post and it looks perfect.can anyone please help me to understand this and help me to get the setup for third sunday of every month.Thanks. this is... (7 Replies)
Discussion started by: netdbaind
7 Replies

5. UNIX for Dummies Questions & Answers

Script should check and run only on Sunday

Hi All, I have a script, I want to make sure the script should check whether the day is sunday, only then it should run, if it is run other days it should check and exit the script. Kindly help. Thanks in Advance !! (41 Replies)
Discussion started by: nanz143
41 Replies

6. Shell Programming and Scripting

How to get first sunday of the month?

Hi, Please can someone help me in getting first sunday date of a month. i_year=`date +%Y` ny_first_sun_nov=`cal 10 $i_year | sed '/^$/d' |head -3 |tail -1| rev | cut -c1` This works good if the first sunday has a value but not if it is blank and first sunday falls on second week. ... (17 Replies)
Discussion started by: infyanurag
17 Replies

7. Shell Programming and Scripting

run the script for last day of the month

Hello Experts, I have a script which i want to run the on last day of every month. let say I have backup.sh script which i want to run it every month last day. Can anyone please help :confused: thanks (4 Replies)
Discussion started by: aks_1902
4 Replies

8. UNIX for Dummies Questions & Answers

Run script on every second sunday

I was to schedule a script in a crontab after every 15 days specically on every 2nd Sunday. I know that i can schedule on basis of weekdays, but can it be done by skipping in between???:wall: (5 Replies)
Discussion started by: masquerer
5 Replies

9. UNIX for Dummies Questions & Answers

How to Set up a cronjob On 4th Sunday of every Month?

How to Set up a cronjob which will run On 4th Sunday of every Month at 8:00 PM :( (11 Replies)
Discussion started by: tp2115
11 Replies

10. UNIX for Dummies Questions & Answers

Cron job -- to execute at every first Sunday of every month

Dear all How can I schedule the cronjob to be run sometime at every first Sunday at every month? I have the edit the cronjob every month now, thanks (2 Replies)
Discussion started by: shanemcmahon
2 Replies
Login or Register to Ask a Question