2 # cal2day - "parse" appropriate calendar output to match date number
5 # usage: cal2day month day [year]
7 # ORIGINAL *TAG:33239 3:Dec 9 1997:0755:sh.d/cal2day:
11 # Converted to bash v2 syntax by Chet Ramey <chet@po.cwru.edu>
14 while getopts :dls _inst
16 (d
) format
='%1d%.0s\n' ;; # 0, 1, ..., 7
17 (l
) format
='%0.s%-s\n' ;; # Sunday, Monday, ..., Saturday
18 (s
) format
='%0.s%-.3s\n' ;; # Sun, Mon, ..., Sat
24 ((!$#)) && set -- $
(date '+%m %d')
25 : ${format:='%0.s%-.3s\n'}
26 : ${1:?missing month parameter [1-12]}
27 : ${2:?missing day parameter [1-31]}
29 #3 CALCULATE DAY-OF-WEEK FROM DATE
30 cal $1 ${3:-$(date +%Y)} | gawk
-FX '
31 BEGIN { day="Sunday Monday Tuesday WednesdayThursday Friday Saturday"
33 dayre="(^| )" daynum "( |$)"
35 #NR==2 { print length($0) }
38 dayre { if (match($0, dayre))
39 { #print RSTART, RLENGTH, substr($0, RSTART, RLENGTH)
40 if (daynum<=9 || RSTART==1) RSTART-=1
44 END { # 20/21 char width assumed
45 printf format, RSTART/3, substr(day, RSTART*3+1, 9)
47 ' daynum
=$2 format
=$format -