Re: Display a calendar with week number



Thanks Chris. I am learning how to write shell scripts. This is going
to be a great example.
btw, I also found 'ncal', it's capable of displaying week number with
simple command as 'ncal -w', but it's in column view.

Chris F.A. Johnson wrote:
On 2006-10-17, realguess@xxxxxxxxx wrote:
cal
October 2006
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

If I would like to diplay a calendar with week number, e.g.

October 2006
Mo Tu We Th Fr Sa Su
40 2 3 4 5 6 7 8
41 9 10 11 12 13 14 15
42 16 17 18 19 20 21 22
43 23 24 25 26 27 28 29
44 30 31

What's the command? or what should I use?

It may be possible with gcal (it has a bewildering number of
options), but I found it easier to write this script than fight
through gcal's info jungle.

You can select the month to display either by number or name.

## wcal: Display calendar for a month, week numbers prepended
## USAGE: wcal [month] [year]
## NOTE: If a single number is given,
## it is a month if it is 12 or less, a year if greater than 12

case $1 in
*[!0-9]*) ## Month is given in name form
case ${1#0} in
[Jj][aA][nN]*) month=1 ;;
[Ff][Ee][Bb]*) month=2 ;;
[Mm][Aa][Rr]*) month=3 ;;
[Aa][Pp][Rr]*) month=4 ;;
[Mm][Aa][Yy]*) month=5 ;;
[Jj][Uu][Nn]*) month=6 ;;
[Jj][Uu][Ll]*) month=7 ;;
[Aa][Uu][Gg]*) month=8 ;;
[Ss][Ee][Pp]*) month=9 ;;
[Oo][Cc][Tt]*) month=10 ;;
[Nn][Oo][Vv]*) month=11 ;;
[Dd][Ee][Cc]*) month=12 ;;
esac
year=$2
;;
*) month=$1 ## Month, if any, is numerical
if [ ${month:-13} -le 12 ]
then
year=$2
else
year=$1
month=
fi
;;
esac

## Fill in defaults if year or month is missing
[ -z "$year" ] || [ -z "$month" ] &&
eval "$(date "+ YEAR=%Y MONTH=%m DAY=%d" )"

## Get week of year
week=$( date -d "${year:-$YEAR}-${month:-$MONTH}-1" +%U )
week=${week#0} ## remove leading zero (for arithmetic in loop)

IFS='
'

## Store calendar in array
month=( $( cal -1 ${month:-$MONTH} ${year:-$YEAR} ) )

## Print month and year, and day headers
printf "%s\n" " ${month[0]}" "Wk ${month[1]}"

unset month[0] month[1]

## Loop though array, incrementing week
for wk in "${month[@]}"
do
[ -n "$wk" ] && printf "%2d %s\n" "$week" "$wk"
week=$(( $week + 1 ))
done

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
My code in this post, if any, is copyright 2006, Chris F.A. Johnson,
and is released under the GNU General Public Licence

.



Relevant Pages

  • Calendar dialog, anyone?
    ... I need a dialog where the user can pick a date from a calendar. ... dialog shall display a whole month, and the user selects a day by ... Dose it exist in any of Sun's librarys, or can anyoune point to one? ... Chris ...
    (comp.lang.java.programmer)
  • Re: User Display names
    ... I'm guessing Chris is referrring to the ... CN which you can't fix with adsiedit anyway. ... > How to Change Display Names of Active Directory Users ... The docuemnt includes a simple script example. ...
    (microsoft.public.windows.server.migration)
  • Re: 2006 Spode of the Year
    ... classic sense), or does lots of "Spodely Acts" or ... I nominate Chris as RMDer of the Year for, literally, being thrown ... courage during his lengthy and painful recovery, ... the ability to have and display such a positive attitude and sense of ...
    (rec.motorcycles.dirt)
  • Re: HELP!!!! Subreports
    ... MS Access MVP ... > "Chris" wrote in message ... >> insert it more times as a subreport setting it for every insertion to ... >> display another consecutive page(and not to appear anymore if there are ...
    (microsoft.public.access.reports)
  • Re: HELP!!!! Subreports
    ... MS Access MVP ... > "Chris" wrote in message ... >> insert it more times as a subreport setting it for every insertion to ... >> display another consecutive page(and not to appear anymore if there are ...
    (microsoft.public.access.queries)