Get Last working day of the month

Hi

I need a script to get "Last working day of the month". I will pass the month and year as parameters and i need to get the last working date.

Ex
for June 2008 the last working day is 30th its monday.
for August 2008 the last working day is 29th and it is Friday.

ie the last working day falls in Monday to Friday.

Thanks for the help.

The only useful Google result I could find was this, but then, it's actually pretty useful. Scroll down to the end for the grande finale. crontab(s) for the last "work-day" of every month [Archive] - The macosxhints Forums

Thanks a lot but i tried that code its going into infinite loop.

I would try something like this:

cal | awk '/^.[0-9]/{nf=NF;Date=$NF}
END{print nf, Date}'
2 30

you will need to modify the END logic, but basically
if nf is between 2 and 6 (representing Monday thru Friday), there is your end of month.

if nf is 1 or 7, that represents Sunday and Saturday, respectively, so adjust the Date according.

Should be a snap.

Edit: My code is garbage....go with the above post. :wink:

This will do the job.

cal 8 2008 | awk '{ if(NF>1) a=$NF ; if (NF==7) a=$6}END{print a}'

With GNU date (coreutils) and bash:

#!/bin/bash 

export LC_ALL=C
m=$(($1+1))
i=1
lastwday=$(date "+%w" --date "2008/$m/01 -$i days")
while [[ "$lastwday"  < 1 ]] || [[ "$lastwday" > 5 ]];do
        ((i++))
        lastwday=$(date "+%w" --date "2008/$m/01 -$i days")
done

dtelastwday=$(date --date "2008/$m/01 -$i days")

echo "The last working day of 2008/$1 is $dtelastwday"
exit 0
$ script-name 8
> The last working day of 2008/8 is Fri Aug 29 00:00:00 CEST 2008