Visit The New, Modern Unix Linux Community


How to find second and fourth Monday of the month?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to find second and fourth Monday of the month?
# 1  
How to find second and fourth Monday of the month?

Hi,

I have came across the scenario where, we have to run the script on second and fourth Monday of each month.

I have tried to search man page of date and also forum for it but, could not get any answer to this.

Can you please advise how can we get second and fourth Monday of the month?

Also, While searching for above problem, I have came across the thread Can we get every tuesday or monday's date for the current week which has following code to find Monday of the current week.

Code:
cal | awk -v d="$(date +"%m %d %y")" 'BEGIN{split(d,a)} $0 ~ int(a[2]) {print a[1] $2 a[3]}'

I am unable to understand that code and that thread is closed. Can someone please explain this code?

Thanks in advance for your help.

Last edited by rbatte1; 06-30-2015 at 07:11 AM.. Reason: Corrected link
# 2  
cal without any further parameters prints the current month in 4 - 5 lines plus two header lines. In these lines, the awk searches for today and, if found, prints $2, the second field, which is Monday as the weeks in cal start with Sunday.
# 3  
You probably want that script to be executed by cron. https://www.unix.com/man-page/opensolaris/1m/cron/

But also see the crontab entry: https://www.unix.com/man-page/centos/5/crontab/

Code:
*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of        month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

So this might be what you want: (untested)
Code:
* * * * 1/2,4 /path/to/script

This suggestion is based on what i read myself just now about cron.
hth
# 4  
With GNU date and a recent bash, try this:
Code:
for MONTH in {01..12}
    do printf "%2s.%2s.%4s      %2s.%2s.%4s\n" \
                $(($(date -d"2015${MONTH}01" +"16 - %u - (%u==1?7:0)"))) $MONTH 2015 \
                $(($(date -d"2015${MONTH}01" +"30 - %u - (%u==1?7:0)"))) $MONTH 2015
    done
12.01.2015    26.01.2015
 9.02.2015    23.02.2015
 9.03.2015    23.03.2015
13.04.2015    27.04.2015
11.05.2015    25.05.2015
 8.06.2015    22.06.2015
13.07.2015    27.07.2015
10.08.2015    24.08.2015
14.09.2015    28.09.2015
12.10.2015    26.10.2015
 9.11.2015    23.11.2015
14.12.2015    28.12.2015

Feel free to adapt to other years.
# 5  
Quote:
Originally Posted by sea
You probably want that script to be executed by cron. https://www.unix.com/man-page/opensolaris/1m/cron/

But also see the crontab entry: https://www.unix.com/man-page/centos/5/crontab/

Code:
*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of        month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

So this might be what you want: (untested)
Code:
* * * * 1/2,4 /path/to/script

This suggestion is based on what i read myself just now about cron.
hth
Thanks for your reply. But, I do not want to use cron. I want to control second and fourth Monday through shell script.

---------- Post updated at 12:50 AM ---------- Previous update was at 12:48 AM ----------

Quote:
Originally Posted by RudiC
With GNU date and a recent bash, try this:
Code:
for MONTH in {01..12}
    do printf "%2s.%2s.%4s      %2s.%2s.%4s\n" \
                $(($(date -d"2015${MONTH}01" +"16 - %u - (%u==1?7:0)"))) $MONTH 2015 \
                $(($(date -d"2015${MONTH}01" +"30 - %u - (%u==1?7:0)"))) $MONTH 2015
    done
12.01.2015    26.01.2015
 9.02.2015    23.02.2015
 9.03.2015    23.03.2015
13.04.2015    27.04.2015
11.05.2015    25.05.2015
 8.06.2015    22.06.2015
13.07.2015    27.07.2015
10.08.2015    24.08.2015
14.09.2015    28.09.2015
12.10.2015    26.10.2015
 9.11.2015    23.11.2015
14.12.2015    28.12.2015

Feel free to adapt to other years.
Thanks. I will try this. But, can you please explain the logic used here.
# 6  
date's %u format specifier supplies the day-of-week of the first day in the target month, which then is subtracted from 16 (for second week) or 30 (4. wk, experimental values). Should the First be a Monday, subtract another 7 days.
# 7  
Getting below errors, any suggestions?
Code:
$ for MONTH in {01..12}
>     do printf "%2s.%2s.%4s      %2s.%2s.%4s\n" \
>                 $(($(date -d"2015${MONTH}01" +"16 - %u - (%u==1?7:0)"))) $MONTH 2015 \
>                 $(($(date -d"2015${MONTH}01" +"30 - %u - (%u==1?7:0)"))) $MONTH 2015
>     done
date: invalid date `2015101'
date: invalid date `2015101'
 0. 1.2015       0. 1.2015
date: invalid date `2015201'
date: invalid date `2015201'
 0. 2.2015       0. 2.2015
date: invalid date `2015301'
date: invalid date `2015301'
 0. 3.2015       0. 3.2015
date: invalid date `2015401'
date: invalid date `2015401'
 0. 4.2015       0. 4.2015
date: invalid date `2015501'
date: invalid date `2015501'
 0. 5.2015       0. 5.2015
date: invalid date `2015601'
date: invalid date `2015601'
 0. 6.2015       0. 6.2015
date: invalid date `2015701'
date: invalid date `2015701'
 0. 7.2015       0. 7.2015
date: invalid date `2015801'
date: invalid date `2015801'
 0. 8.2015       0. 8.2015
date: invalid date `2015901'
date: invalid date `2015901'
 0. 9.2015       0. 9.2015
12.10.2015      26.10.2015
 9.11.2015      23.11.2015
14.12.2015      28.12.2015
$

 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #418
Difficulty: Easy
'JavaScript' is a trademark of Oracle Corporation in the United States.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find one month before date

Hi, I want two dates one will be the current date and the other one will be just one month before. Say if current month is 11/4/2014 then the other date should be 11/3/2014. #!/bin/ksh currentDtae=`date` oneMonthBefore= ? I dont know how to do it. Went through some of the related threads... (15 Replies)
Discussion started by: Sharma331
15 Replies

2. UNIX for Advanced & Expert Users

How to find last two files for a month?

Hi All, I need to find last two files for the month. lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 15:43 c.txt -rwxr-xr-x 1 user userg 1596 Mar 21 15:43 d.txt -rwxr-xr-x 1 user userg 1596 Mar 22 15:43 f.txt -rwxr-xr-x 1... (14 Replies)
Discussion started by: Makarand Dodmis
14 Replies

3. HP-UX

How to find a file created in UNIX every monday.???

Hi All Any one please suggest me... I have one directory every monday one file will be created in that directory. so if the file is created on monday or not i need check first. How can write a script??? if the file is not created i want to quit from script. Thanks K.Srinivas (5 Replies)
Discussion started by: k_s_rao7
5 Replies

4. UNIX for Advanced & Expert Users

Crontab For First Monday Of Every Month!!

Hi, Could any one please let me know the crontab entry for scheduling a job for every first monday of the month? Thank You in advance, Sue (2 Replies)
Discussion started by: pyaranoid
2 Replies

5. UNIX for Dummies Questions & Answers

cron script -run every 2nd day of month except Monday

I know I can't schedule this in cron and would have to write a wrapper around my script and schedule it in cron ....but not sure how do to this? How do I exclude Monday if the 2nd day of the month falls on a Monday? Thanks. I tried this: 0 0 2 * 0,2-6 command And I know this doesnt... (2 Replies)
Discussion started by: newtou
2 Replies

6. UNIX for Dummies Questions & Answers

I wanted to get the date of the first monday of a month.

Hi, I need to display the date of the first monday of a month. Can any one please help me on this. Thanks in advance. (6 Replies)
Discussion started by: Sheethal
6 Replies

7. UNIX for Dummies Questions & Answers

find out month from a date

I would like to find out the month from a given date, how is it possible. (5 Replies)
Discussion started by: rudoraj
5 Replies

8. Shell Programming and Scripting

how can i find the third friday of each month?

Help please! I need to read the calendar and put the date of the third Friday of each month into a variable for comparison in an "if" statement. How would I do this? Thnx, leslie02 (10 Replies)
Discussion started by: leslie02
10 Replies

9. Shell Programming and Scripting

Find all files by month

Hi, I am trying to do achieving of files by months. find /test -name \*.* -mtime +30 will give me the result of all modified files after 30 days. But lets say i want to list all files that is modified in last months... what is the command to do it? Thanks! (13 Replies)
Discussion started by: maldini
13 Replies

10. UNIX for Dummies Questions & Answers

Crontab First Monday of Month only

Is there a way to setup a cronjob that will only run on the first monday of the month? (3 Replies)
Discussion started by: molonede
3 Replies

Featured Tech Videos