Re: Display a calendar with week number
- From: "realguess" <realguess@xxxxxxxxx>
- Date: 17 Oct 2006 17:03:12 -0700
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:
calOctober 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
.
- References:
- Display a calendar with week number
- From: realguess@xxxxxxxxx
- Re: Display a calendar with week number
- From: Chris F.A. Johnson
- Display a calendar with week number
- Prev by Date: A new reader? Welcome to alt.os.linux, read this first if you're new here (FAQ)
- Next by Date: network help getting iperf to work
- Previous by thread: Re: Display a calendar with week number
- Next by thread: Re: Display a calendar with week number
- Index(es):
Relevant Pages
|