bf1873eb86c1a75acf085896feb571a6e6b19e7c
[BK-2020-03.git] / unitproc / bkt-read_stdin
1 #!/usr/bin/env bash
2 # Desc: Reads stdin
3
4 yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
5 die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
6 must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
7 read_stdin() {
8 # Desc: Consumes stdin; outputs as stdout lines
9 # Input: stdin (consumes)
10 # Output: stdout (newline delimited)
11 # return 0 stdin read
12 # return 1 stdin not present
13 # Example: printf "foo\nbar\n" | read_stdin
14 # Depends: GNU bash (version 5.1.16), GNU Coreutils 8.32 (cat)
15 # Version: 0.1.0
16 # Attrib: Steven Baltakatei Sandoval (2024-01-29). reboil.com
17 local input_stdin output;
18
19 # Store stdin
20 if [[ -p /dev/stdin ]]; then
21 input_stdin="$(cat -)";
22 else
23 return 1;
24 fi;
25
26 # Store as output array elements
27 ## Read in stdin
28 if [[ -n $input_stdin ]]; then
29 while read -r line; do
30 output+=("$line");
31 done < <(printf "%s\n" "$input_stdin");
32 fi;
33
34 # Print to stdout
35 printf "%s\n" "${output[@]}";
36
37 return 0;
38 }; # read stdin to stdout lines
39
40 yell "WARNING:This is a Bash function definition for read_stdin.";