Commit | Line | Data |
---|---|---|
335b71e1 SBS |
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 | # Example: printf "foo\nbar\n" | read_stdin | |
12 | # Depends: GNU bash (version 5.1.16) | |
13 | # Version: 0.0.1 | |
14 | local input_stdin output; | |
15 | ||
16 | # Store stdin | |
17 | if [[ -p /dev/stdin ]]; then | |
18 | input_stdin="$(cat -)"; | |
19 | fi; | |
20 | ||
21 | # Store as output array elements | |
22 | ## Read in stdin | |
23 | if [[ -n $input_stdin ]]; then | |
24 | while read -r line; do | |
25 | output+=("$line"); | |
26 | done < <(printf "%s\n" "$input_stdin"); | |
27 | fi; | |
28 | ||
29 | # Print to stdout | |
30 | printf "%s\n" "${output[@]}"; | |
31 | }; # read stdin to stdout lines | |
32 | main() { | |
33 | read_stdin "$@"; | |
34 | }; | |
35 | ||
36 | main "$@"; |