Patch-ID: bash40-021
[bash.git] / examples / scripts.v2 / cal2day.bash
blobf26128b6687787277a8f901d14730b3a221f8f3d
1 #!/bin/bash
2 # cal2day - "parse" appropriate calendar output to match date number
3 # with day name.
5 # usage: cal2day month day [year]
7 # ORIGINAL *TAG:33239 3:Dec 9 1997:0755:sh.d/cal2day:
9 # Obtained from usenet
11 # Converted to bash v2 syntax by Chet Ramey <chet@po.cwru.edu>
13 #1 PARSE OPTIONS
14 while getopts :dls _inst
15 do case $_inst in
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
19 esac
20 done
21 shift $((OPTIND-1))
23 #2 PARAMETER VALUES
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"
32 sub(/^0/, "", daynum)
33 dayre="(^| )" daynum "( |$)"
35 #NR==2 { print length($0) }
36 NR==1 || NR==2 \
37 { next }
38 dayre { if (match($0, dayre))
39 { #print RSTART, RLENGTH, substr($0, RSTART, RLENGTH)
40 if (daynum<=9 || RSTART==1) RSTART-=1
41 exit
44 END { # 20/21 char width assumed
45 printf format, RSTART/3, substr(day, RSTART*3+1, 9)
47 ' daynum=$2 format=$format -
49 exit 0