The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Use date command to find last month Cbish68 Shell Programming and Scripting 5 08-10-2007 10:32 AM
find out month from a date rudoraj UNIX for Dummies Questions & Answers 5 07-03-2007 08:21 AM
How to find the first day of previous month in unix? mohapatra Shell Programming and Scripting 10 07-02-2007 08:57 PM
Need help, Every friday in a month LAY Shell Programming and Scripting 3 12-09-2006 09:12 AM
Find all files by month maldini Shell Programming and Scripting 13 08-03-2005 01:22 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-30-2007
leslie02 leslie02 is offline
Registered User
  
 

Join Date: May 2007
Posts: 5
Smile 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
  #2 (permalink)  
Old 05-30-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,355
The third friday is always between days 15 and 21 inclusive of the month .
You can made the test like that :
Code:
day_of_week=$(date +%w)  # (0..6); 0 is Sunday, 5 is Friday
day_of_month=$(date +%e) # space padded
if [ ${day_of_week} -eq 5 -a ${day_of_month} -ge 15 -a ${day_of_month} -le 21 ]
then
   echo "third Friday "
fi
Jean-Pierre.
  #3 (permalink)  
Old 05-30-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Leslie,
See if this works for you:
Code:
typeset -i mMth=1
mYear='2007'
while [ ${mMth} -le 12 ]
do
  m3Friday=`cal ${mMth} ${mYear} | tail +3 | cut -c16,17 | sed '/^ *$/d' | sed -n '3p'`
  echo "Third Friday of "${mMth}"/"${mYear}" = "${m3Friday}
  mMth=${mMth}+1
done
  #4 (permalink)  
Old 05-31-2007
akrathi akrathi is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 41
This should work

Thanks
Ashok

cal 07 2007 | cut -c16-18 | tail -4 | head -1
  #5 (permalink)  
Old 05-31-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
cal 05 2007 | sed -n "5{s/ *[0-9]\{2\}$//;s/^.* //p;}"
  #6 (permalink)  
Old 05-31-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,355
Shell_Life, akrathi, anbu23, it seems that your solutions do not work in all the cases :
Code:
$ cal 12 2007
       December 2007      
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

$ cal 12 2007 | tail +3 | cut -c16,17 | sed '/^ *$/d' | sed -n '3p'
 2
$ cal 12 2007 | cut -c16-18 | tail -4 | head -1
 13
$ cal 12 2007 | sed -n "5{s/ *[0-9]\{2\}$//;s/^.* //p;}"
14
$
A possible solution :
Code:
$ cal 12 2007 | awk 'NR>2 && NF>=2 && ++w==3 {print $6}'
21
$ cal 07 2007 | awk 'NR>2 && NF>=2 && ++w==3 {print $6}'
20
$ cal 6 02007 | awk 'NR>2 && NF>=2 && ++w==3 {print $6}' 
15
$
Jean-Pierre.
  #7 (permalink)  
Old 05-31-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
this one doesn't use cal
Code:
count=0
month="05"
for s in `seq 1 31`
do
 fr=$(date +%a --date="2007-$month-$s" )
 if [ "$fr" == "Fri" ];then
     friday[$count]=$s
     count=$((count+1))
 fi 
done
echo ${friday[2]} #third fri
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 08:37 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0