chore(user/bkmml):Cleanup comment
[BK-2020-03.git] / unitproc / bkt-read_stdin_psarg
CommitLineData
b04a0c0f 1#!/usr/bin/env bash
54e7b419 2# Desc: Reads stdin and positional arguments
b04a0c0f
SBS
3
4yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
5die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
6must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
7read_stdin_psarg() {
8 # Desc: Consumes stdin and reads arguments; outputs as stdout lines
9 # Input: stdin (consumes)
10 # Input: args
11 # Output: stdout (newline delimited)
12 # Example: read_stdin_psarg "$@"
13 # Depends: GNU bash (version 5.1.16)
966a8d11 14 # Version: 0.0.3
b04a0c0f
SBS
15 local input_stdin input_psarg output;
16
17 # Store stdin
18 if [[ -p /dev/stdin ]]; then
19 input_stdin="$(cat -)";
20 fi;
b04a0c0f
SBS
21
22 # Store arguments
23 if [[ $# -gt 0 ]]; then
966a8d11 24 input_psarg="$*";
b04a0c0f 25 fi;
b04a0c0f
SBS
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;
b04a0c0f
SBS
40
41 # Print to stdout
42 printf "%s\n" "${output[@]}";
43}; # read stdin and positional argument to stdout lines
b04a0c0f
SBS
44main() {
45 read_stdin_psarg "$@";
46};
47
48main "$@";