| 1 | #!/bin/bash
|
|---|
| 2 | # cal2day - "parse" appropriate calendar output to match date number
|
|---|
| 3 | # with day name.
|
|---|
| 4 | #
|
|---|
| 5 | # usage: cal2day month day [year]
|
|---|
| 6 | #
|
|---|
| 7 | # ORIGINAL *TAG:33239 3:Dec 9 1997:0755:sh.d/cal2day:
|
|---|
| 8 | #
|
|---|
| 9 | # Obtained from usenet
|
|---|
| 10 | #
|
|---|
| 11 | # Converted to bash v2 syntax by Chet Ramey <[email protected]>
|
|---|
| 12 |
|
|---|
| 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))
|
|---|
| 22 |
|
|---|
| 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]}
|
|---|
| 28 |
|
|---|
| 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 "( |$)"
|
|---|
| 34 | }
|
|---|
| 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
|
|---|
| 42 | }
|
|---|
| 43 | }
|
|---|
| 44 | END { # 20/21 char width assumed
|
|---|
| 45 | printf format, RSTART/3, substr(day, RSTART*3+1, 9)
|
|---|
| 46 | }
|
|---|
| 47 | ' daynum=$2 format=$format -
|
|---|
| 48 |
|
|---|
| 49 | exit 0
|
|---|