X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/blobdiff_plain/966a8d1189ee6e224fcc4cbee330c3e6df7fb077..335b71e1a32629a461b224075a4d83a19dd376cb:/unitproc/bkt-read_stdin diff --git a/unitproc/bkt-read_stdin b/unitproc/bkt-read_stdin new file mode 100644 index 0000000..31469b3 --- /dev/null +++ b/unitproc/bkt-read_stdin @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +# Desc: Reads stdin + +yell() { echo "$0: $*" >&2; } # print script path and all args to stderr +die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status +must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails +read_stdin() { + # Desc: Consumes stdin; outputs as stdout lines + # Input: stdin (consumes) + # Output: stdout (newline delimited) + # Example: printf "foo\nbar\n" | read_stdin + # Depends: GNU bash (version 5.1.16) + # Version: 0.0.1 + local input_stdin output; + + # Store stdin + if [[ -p /dev/stdin ]]; then + input_stdin="$(cat -)"; + fi; + + # Store as output array elements + ## Read in stdin + if [[ -n $input_stdin ]]; then + while read -r line; do + output+=("$line"); + done < <(printf "%s\n" "$input_stdin"); + fi; + + # Print to stdout + printf "%s\n" "${output[@]}"; +}; # read stdin to stdout lines +main() { + read_stdin "$@"; +}; + +main "$@";