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.:wall:

pls help me.

Dosanjh

look at this thread.

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.

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:

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

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:

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
2 Likes

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

If you are using ksh93

$ printf "%T\n" "this friday + 2 weeks"
Fri Sep 7 00:00:00 GMT+0800 2012 

another one to calculate the 3rd friday date

 
$ 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
2 Likes

Good idea how about:

cal | awk ' --NF&&$NF+0{print $NF+14; exit}'

If a little extra tool is fine have a look at dateutils (http://hroptatyr.github.com/dateutils/[](https://hroptatyr.github.com/dateutils/)\). What you want:

dconv 2012-08-03-05 -f '%Y-%m-%d'
=>
2012-08-17

which makes use of a special calendar system to denote every day as year-month-count-weekday, where count is the occurrence of weekday in month (here 3) and weekday is a number 00 for Sun, 01 for Mon, etc.