[clug] CLUG meeting 21 August 2003

Nemo -earth native- nemo at cheeky.house.cx
Mon Aug 18 15:11:22 EST 2003


On Mon, Aug 18, 2003 at 07:51:54AM +1000, Brett Worth did utter:
> 
> thurs = `cal $mon $year | awk ' NF >= 5 { t = $5 } END { print t } '`
> 


$ cal 10 2003 | awk ' NF >= 5 { t = $5 } END { print t } '
30


:(

dmittedly, my first idea on how to calculate the 4th thursday of the
month was to just run date in a loop testing for each day of hte month
in sequence from the 1st untill I find the first thirsday, and then add
21 days. 

No parsing of `cal` at all, but requires multiple (up to 7) spawnings of
`date`. 

...

On second reflection, it's just really a fairly simple lookup table. 

----
FIRST_OF_MONTH=$(date --date="01-$MONTH-$YEAR" +%a)
case "$FIRST_OF_MONTH" in
  Mon) FOURTH_THURS=25 ;;
  Tue) FOURTH_THURS=24 ;;
  Wed) FOURTH_THURS=23 ;;
  Thu) FOURTH_THURS=22 ;;
  Fri) FOURTH_THURS=28 ;;
  Sat) FOURTH_THURS=27 ;;
  Sun) FOURTH_THURS=26 ;;
esac
----

It's more code, but it's clearer (imho) what's happening, and actually
requires less spawning of outside processes, not to mention not relying
on the formatting output of another program. 

(note, I've not tested this code beyond the `date` command :)

.../Nemo
-- 
  ------------------------------------------ --------------------------
                                                    earth native



More information about the linux mailing list