Last Friday of each month: Difference between revisions

(Javascript Code)
Line 2,157:
echo $1-$m-`ncal $m $1 | grep Fr | sed 's/.* \([0-9]\)/\1/'`
done</lang>
 
 
For systems without ncal:
<lang sh>#!/bin/sh
 
# usage: last_fridays [ year]
 
year=${1:-`date +%Y`} # default to current year
month=1
while [ 12 -ge $month ]; do
# Ensure 2 digits: if we try to strip off 2 characters but it still
# looks the same, that means there was only 1 char, so we'll pad it.
[ "$month" = "${month%??}" ] && month=0$month
 
# The first line prints only the last 2 weeks of the month.
#
# The second line prints only weeks containing a Friday,
# stripping off all days before Friday.
#
# The third line strips any Saturday, adds the year and month,
# and prints only the last Friday.
cal $month $year | grep . | tail -2 \
| sed -n 's@^\([0-9]\{1,2\} \)\{5\}@@p' \
| sed -ne 's@ .*@@' -e "s@^@$year-$month-@" -e '$p'
 
# Strip leading zeros to avoid octal interpretation
month=$(( 1 + ${month#0} ))
done</lang>
 
 
Using <code>date --date</code> from GNU date??? This code is not portable.
Anonymous user