2 # Desc: Checks stdin for lines out of order.
7 yell
() { echo "$0: $*" >&2; } # print script path and all args to stderr
8 die
() { yell
"$*"; exit 111; } # same as yell() but non-zero exit status
9 must
() { "$@" || die
"cannot $*"; } # runs args as command, reports args if command fails
11 # Desc: Consumes stdin; outputs as stdout lines
12 # Input: stdin (consumes)
13 # Output: stdout (newline delimited)
15 # return 1 stdin not present
16 # Example: printf "foo\nbar\n" | read_stdin
17 # Depends: GNU bash (version 5.1.16), GNU Coreutils 8.32 (cat)
19 # Attrib: Steven Baltakatei Sandoval (2024-01-29). reboil.com
20 local input_stdin output
;
23 if [[ -p /dev
/stdin
]]; then
24 input_stdin
="$(cat -)" ||
{
25 echo "FATAL:Error reading stdin." 1>&2; return 1; };
30 # Store as output array elements
32 if [[ -n $input_stdin ]]; then
33 while read -r line
; do
35 done < <(printf "%s\n" "$input_stdin") ||
{
36 echo "FATAL:Error parsing stdin."; return 1; };
40 printf "%s\n" "${output[@]}";
43 }; # read stdin to stdout lines
47 while IFS
= read -r line
; do
49 if [[ -n "$previous" ]] && [[ "$previous" > "$current" ]]; then
50 yell
"STATUS:Line $n is out of order: $line";
54 done < <(read_stdin || die
"FATAL:No stdin.");
60 # Author: Steven Baltakatei Sandoval