how to get 3rd week of every friday?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to get 3rd week of every friday?
# 1  
Old 08-22-2012
how to get 3rd week of every friday?

Legends,

how do i get 3rd week of friday in every month and execute a particular script say /tmp/abc.sh ???

i think after "cal" we can traverse through using some for loop.Smilie

pls help me.

Dosanjh
# 2  
Old 08-22-2012
look at this thread.
# 3  
Old 08-22-2012
Because the date command and cron are critical to this process, please post what Operating System and version you are running and what Shell you use for system scripts.

Also, please provide a short sequence of sample future dates on which this cron should run.
# 4  
Old 08-22-2012
This should work on most flavours of UNIX depending on how standard the output of cal is.

I've tested it with cal formats like:
Code:
     August 2012    
Su Mo Tu We Th Fr Sa
          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

Code:
        August 2012       
Sun Mon Tue Wed Thu Fri Sat  
             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

Anyway it should give you a good start.

Code:
today=$(date +%d)
tf=$(cal | awk '/^S/{s=index($0,"Fr")} substr($0,s,2)~/[ 1-2][0-9]/{i++} i==3{print substr($0,s,2)}')
if [ "$today" = "$tf" ]
then
    echo "It's the 3rd Friday today"
else
    echo "Today is $today and 3rd Friday is $tf"
fi


If your date command supports -d you could use:

Code:
today=$(date +%d)
month=$(date -d $(date +%m)/01/$(date +%Y) +%m/%d/%Y)
tf=$(date -d "$month +$(( 20 - ( $(date -d $month +%u) +1)%7))days" +%d)
 
if [ "$today" = "$tf" ]
then
    echo "It's the 3rd Friday today"
else
    echo "Today is $today and 3rd Friday is $tf"
fi


Last edited by Chubler_XL; 08-23-2012 at 12:13 AM..
These 2 Users Gave Thanks to Chubler_XL For This Post:
# 5  
Old 08-22-2012
Thanks Chubler_XL,

1st one works. as "-d" is not supported by OS.
but i think my purpose will be solved by 1st code.
Thanks
# 6  
Old 08-22-2012
If you are using ksh93
Code:
$ printf "%T\n" "this friday + 2 weeks"
Fri Sep 7 00:00:00 GMT+0800 2012

# 7  
Old 08-23-2012
another one to calculate the 3rd friday date

Code:
 
$ cal 12 2012 | nawk 'NR==4{if($6>7){fri=$6+7}else{fri=$6+14}}END{print fri}'      
21
$ cal 01 2012 | nawk 'NR==4{if($6>7){fri=$6+7}else{fri=$6+14}}END{print fri}' 
20
$ cal 11 2012 | nawk 'NR==4{if($6>7){fri=$6+7}else{fri=$6+14}}END{print fri}' 
16
$ cal | nawk 'NR==4{if($6>7){fri=$6+7}else{fri=$6+14}}END{print fri}'
17

These 2 Users Gave Thanks to itkamaraj For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Linux

Bash Display First Friday of the next month

Hello, I need to find the date of next first Friday of the month and set as a variable in a bash script ie - FIRSTFRIDAY=$(date -dfirst-friday +%d) I know date -dfirst-friday doesn't work, but unsure if I can use this / cal + awk or something else to find the right date of the... (7 Replies)
Discussion started by: summerdays
7 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Cron - job to run every 3rd Friday of the month only

Hi Expert Please help me to set a cron job schedule, Ihave a job that run every 3rd Friday of the month at 1030am. I tried to set up like this, but the job still runs every friday at 1030am. I want the job to run every 3rd Friday of the month at 1030am only 30 10 15,16,17,18,19,20,21... (2 Replies)
Discussion started by: kaibiganmi
2 Replies

3. Shell Programming and Scripting

Last friday of every month

Hi, I need to get the date of last friday of every month. how can i achieve this ? please guide me. Thanks in advance (3 Replies)
Discussion started by: apsprabhu
3 Replies

4. Shell Programming and Scripting

pickup until last friday files from directory

Freinds, I need help: I need to process number of flat files on weekly basis. My source dir will receive flat files regulary at mid night between 1:00am and 6:00am. When I run my weekly file process it should get only the files which are until last friday (including friday)and move them in... (1 Reply)
Discussion started by: magi
1 Replies

5. 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

6. Shell Programming and Scripting

Friday afternoon headache

Hi all, It's been a long week and my brain is clearly not functioning right so hopefully someone can help me out here. I've got a function in a script which just checks if a MySQL database directory exists or not. Code is as follows: dbCheck2() { if ; then { : # carry... (4 Replies)
Discussion started by: _Spare_Ribs_
4 Replies

7. Shell Programming and Scripting

Need help, Every friday in a month

I am trying to write a script that shows every Friday in a month. I used cal $1 $2 | grep -v "^$" | awk '{print $6}' It doesn't work for the frist week of Friday because calendar command output has some spaces in the first line and awk '{print $6}' doesn't work. Anybody help me with this... (3 Replies)
Discussion started by: LAY
3 Replies

8. Shell Programming and Scripting

How to identify week-1 and week-2

Hello Friends I have three dirs 1. /home/main-bkup 2. /home/bkup-week1 3. /home/bkup-week2 Now we copy backups in 1 initially, then on 1st week we copy few content of 1 into 2 and then run some scripts on that. Then in 2nd week we keep 2 untouched and do the same thing in 3. So I... (1 Reply)
Discussion started by: csaha
1 Replies
Login or Register to Ask a Question