f072ca6acdba7266ab6489215942bd054f5e56ae
3 # Depends: date (GNU Coreutils 8.32)
5 # Ref/Attrib: [1] Template:This date in recent years https://en.wikipedia.org/wiki/Template:This_date_in_recent_years
6 # [2] Removing leading zeros before passing a shell variable to another command https://stackoverflow.com/a/11130324
8 declare -g yearRange
; # range of years to consider
10 yell
() { echo "$0: $*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique
11 die
() { yell
"$*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370
12 try
() { "$@" || die
"cannot $*"; } #o
14 # Description: Prints verbose message ("vbm") to stderr if opVerbose is set to "true".
15 # Usage: vbm "DEBUG :verbose message here"
20 # Depends: bash 5.1.16, GNU-coreutils 8.30 (echo, date)
22 if [ "$opVerbose" = "true" ]; then
23 functionTime
="$(date --iso-8601=ns)"; # Save current time in nano seconds.
24 echo "[$functionTime]:$0:""$*" 1>&2; # Display argument text.
28 return 0; # Function finished.
29 } # Displays message if opVerbose true
31 # Desc: Display script usage information
36 # Depends: GNU-coreutils 8.30 (cat)
39 bk_this-date-in-recent-years.sh [ options ] [str day of month]
43 Display help information.
45 Display script version.
47 Display debugging info.
49 Specify year range (iso-8601 period), e.g.
50 "2010/2020". By default, most recent 10
53 Indicate end of options.
56 bk_this_date-in-recent-years.sh "05-11"
57 bk_this_date-in-recent-years.sh -r "1990/2020" "05-11"
58 bk_this_date-in-recent-years.sh -r "1990/2020" -- "05-11"
60 } # Display information on how to use this script.
62 # Desc: Processes arguments provided to script.
63 # Usage: processArgs "$@"
65 # Input: "$@" (list of arguments provided to the function)
66 # yearRange Iso date range to consider (e.g. "1999/2020").
67 # Output: Sets following variables used by other functions:
68 # opVerbose Indicates verbose mode enable status. (ex: "true", "false")
69 # opIsoRange Indicates that an iso-8601 date range is specified. (ex: "true", "false")
70 # yearRange Iso date range to consider (e.g. "1999/2020").
71 # arrayPosArgs Array of remaining positional argments
73 # yell() Displays messages to stderr.
74 # vbm() Displays messsages to stderr if opVerbose set to "true".
75 # showUsage() Displays usage information about parent script.
76 # showVersion() Displays version about parent script.
77 # arrayPosArgs Global array for storing non-option positional arguments (i.e. arguments following the `--` option).
78 # External dependencies: bash (5.1.16), echo
80 # [1]: Marco Aurelio (2014-05-08). "echo that outputs to stderr". https://stackoverflow.com/a/23550347
81 # [2]: "Handling positional parameters" (2018-05-12). https://wiki.bash-hackers.org/scripting/posparams
84 vbm
"DEBUG:processArgs function called."
87 while [ ! $# -eq 0 ]; do # While number of arguments ($#) is not (!) equal to (-eq) zero (0).
88 #yell "DEBUG:Starting processArgs while loop." # Debug stderr message. See [1].
89 #yell "DEBUG:Provided arguments are:""$*" # Debug stderr message. See [1].
91 -h |
--help) showUsage
; exit 1;; # Display usage.
92 --version) showVersion
; exit 1;; # Show version
93 -v |
--verbose) opVerbose
="true"; vbm
"DEBUG:Verbose mode enabled.";; # Enable verbose mode. See [1].
95 opIsoRange
="true"; vbm
"DEBUG:Accepting iso-8601 year range."; # Accept iso-8601 year range.
98 --) # End of all options. See [2].
101 vbm
"DEBUG:adding to arrayPosArgs:$arg";
102 arrayPosArgs
+=("$arg");
105 -*) showUsage
; yell
"ERROR: Unrecognized option."; exit 1;; # Display usage
106 *) for arg
in "$@"; do
107 vbm
"DEBUG:adding to arrayPosArgs:$arg";
108 arrayPosArgs
+=("$arg");
116 vbm
"DEBUG:processArgs function ended."
117 return 0; # Function finished.
118 } # Evaluate script options from positional arguments (ex: $1, $2, $3, etc.).
120 # Input: arrayPosArgs array with positional arguments
121 # opVerbose bool indicates to vbm() to display verbose text
122 # opIsoRange bool indicates to use a user-provided year range
123 # yearRange str iso-8601 year range provided by -r, --iso-range option
125 # Validate year $range
126 re
='^[0-9]+/[0-9]+$'; # Simply check (int)/(int). Don't address Gregorian Calendar here.
127 if [[ $opIsoRange == "true" && ! "$yearRange" =~
$re ]] ; then
129 yell
"ERROR:Not a valid date range (e.g. 2010/2022):$yearRange";
133 }; # Validate arguments
135 # Input: arrayPosArgs array with positional arguments
136 # opVerbose bool indicates to vbm() to display verbose text
137 # opIsoRange bool indicates to use a user-provided year range
138 # yearRange str iso-8601 year range provided by -r, --iso-range option
139 # Output: stdout: dates with days of the week (newline delimited)
140 # Depends: date (GNU Coreutils 8.32)
141 # Depends: processArgs(), yell(), vbm(), showUsage(), showVersion(), validatePosArgs()
142 local dom yc ymin ymax
;
146 if ! validatePosArgs
; then
147 die
"FATAL:Invalid positional arguments:$(declare -p arrayPosArgs)";
151 if [[ ! $opIsoRange == "true" ]]; then
152 ## Default: Most recent 10 years
153 yc
="10"; # count of years to consider
154 ymax
="$(date +%Y)"; # highest year to consider
155 ymin
="$((ymax - yc))"; # earliest year to consider
157 ## Provided via -r,--iso-range option
158 ymin
="$(echo "$yearRange" | cut -d'/' -f1)";
159 ymin
="$((10#$ymin))"; # strip leading zeroes. See [2]
160 ymax
="$(echo "$yearRange" | cut -d'/' -f2)"; # See [2]
161 ymax
="$((10#$ymax))"; # strip leading zeroes. See [2]
162 yc
="$((ymax - ymin + 1))";
164 # yell "DEBUG:ymin:$ymin";
165 # yell "DEBUG:ymax:$ymax";
166 # yell "DEBUG:yc :$yc";
169 if ! [[ $
(( ymax
- ymin
)) -ge 0 ]]; then die
"FATAL:Invalid year range:$yearRange"; fi;
172 ## Get day of month (first positional argument)
173 dom
="${arrayPosArgs[0]}";
174 for (( year
= ymin
; year
<= ymax
; year
++ )); do
176 date +%Y-
%m-
%d\
%A
--date="$dstr";
177 # printf "DEBUG:dom :%s\n" "$dom";
178 # printf "DEBUG:ymax:%s\n" "$ymax";
179 # printf "DEBUG:ymin:%s\n" "$ymin";
180 # printf "DEBUG:year:%s\n" "$year";
181 # printf "DEBUG:dstr:%s\n" "$dstr";
189 # Author: Steven Baltakatei Sandoval
192 # Ref/Attrib (depends)
194 # date (GNU coreutils) 8.32
195 # Copyright (C) 2020 Free Software Foundation, Inc.
196 # License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
197 # This is free software: you are free to change and redistribute it.
198 # There is NO WARRANTY, to the extent permitted by law.
200 # Written by David MacKenzie.