feat(user/bkfeh):Add wrapper for feh
[BK-2020-03.git] / unitproc / bkt-read_stdin_psarg
1 #!/usr/bin/env bash
2 # Desc: Reads stdin and positional arguments
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_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)
14 # Version: 0.0.1
15 local input_stdin input_psarg output;
16
17 # Store stdin
18 if [[ -p /dev/stdin ]]; then
19 input_stdin="$(cat -)";
20 fi;
21 yell "DEBUG:$(declare -p input_stdin)";
22
23 # Store arguments
24 if [[ $# -gt 0 ]]; then
25 input_psarg="$@";
26 fi;
27 yell "DEBUG:$(declare -p input_psarg)";
28
29 # Combine as output array elements
30 ## Read in stdin
31 if [[ -n $input_stdin ]]; then
32 while read -r line; do
33 output+=("$line");
34 done < <(printf "%s\n" "$input_stdin");
35 fi;
36 ## Read in positional arguments
37 if [[ -n $input_psarg ]]; then
38 for arg in "$@"; do
39 output+=("$arg");
40 done;
41 fi;
42 yell "DEBUG:$(declare -p output)";
43
44 # Print to stdout
45 printf "%s\n" "${output[@]}";
46 }; # read stdin and positional argument to stdout lines
47 main() {
48 read_stdin_psarg "$@";
49 };
50
51 main "$@";