Commit | Line | Data |
---|---|---|
b04a0c0f SBS |
1 | #!/usr/bin/env bash |
2 | ||
3 | yell() { echo "$0: $*" >&2; } # print script path and all args to stderr | |
4 | die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status | |
5 | must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails | |
6 | read_stdin_psarg() { | |
7 | # Desc: Consumes stdin and reads arguments; outputs as stdout lines | |
8 | # Input: stdin (consumes) | |
9 | # Input: args | |
10 | # Output: stdout (newline delimited) | |
11 | # Example: read_stdin_psarg "$@" | |
12 | # Depends: GNU bash (version 5.1.16) | |
13 | local input_stdin input_psarg output; | |
14 | ||
15 | # Store stdin | |
16 | if [[ -p /dev/stdin ]]; then | |
17 | input_stdin="$(cat -)"; | |
18 | fi; | |
19 | yell "DEBUG:$(declare -p input_stdin)"; | |
20 | ||
21 | # Store arguments | |
22 | if [[ $# -gt 0 ]]; then | |
23 | input_psarg="$@"; | |
24 | fi; | |
25 | yell "DEBUG:$(declare -p input_psarg)"; | |
26 | ||
27 | # Combine as output array elements | |
28 | ## Read in stdin | |
29 | if [[ -n $input_stdin ]]; then | |
30 | while read -r line; do | |
31 | output+=("$line"); | |
32 | done < <(printf "%s\n" "$input_stdin"); | |
33 | fi; | |
34 | ## Read in positional arguments | |
35 | if [[ -n $input_psarg ]]; then | |
36 | for arg in "$@"; do | |
37 | output+=("$arg"); | |
38 | done; | |
39 | fi; | |
40 | yell "DEBUG:$(declare -p output)"; | |
41 | ||
42 | # Print to stdout | |
43 | printf "%s\n" "${output[@]}"; | |
44 | }; # read stdin and positional argument to stdout lines | |
45 | ||
46 | main() { | |
47 | read_stdin_psarg "$@"; | |
48 | }; | |
49 | ||
50 | main "$@"; |