Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- function simpleTest() {
- printf '%s\n' 'This should appear on stderr' >&2;
- printf '%s\n' 'This should appear on stdout';
- return 42
- }
- function simpleColorTest() {
- local red="$(tput setaf 1)";
- local green="$(tput setaf 2)";
- local magenta="$(tput setaf 5)";
- local cyan="$(tput setaf 6)"
- local normal="$(tput sgr0)";
- printf '%s\n' "${red}ERROR:${normal} This should appear on ${magenta}stderr${normal}" >&2;
- printf '%s\n' "${green}PASS:${normal} This should appear on ${cyan}stdout${normal}";
- return 42
- }
- function realTimeStdoutTest() {
- # simulate realtime output on stdout, similar to what you might get from
- # traceroute reddit.com
- # but faster and with less dependencies
- local i n=5;
- for(( i=1; i <= n; i++)); do
- sleep 0.5s;
- printf '%s\n' "stdout $i of $n";
- done
- return 33
- }
- function realTimeColoredStdoutTest() {
- # simulate realtime output on stdout, similar to what you might get from
- # traceroute reddit.com
- # but faster and with less dependencies
- local red="$(tput setaf 1)";
- local green="$(tput setaf 2)";
- local magenta="$(tput setaf 5)";
- local cyan="$(tput setaf 6)"
- local normal="$(tput sgr0)";
- local i n=5;
- for(( i=1; i <= n; i++)); do
- sleep 0.5s;
- printf '%s\n' "${cyan}stdout${normal} ${green}${i}${normal} of ${magenta}${n}${normal}";
- done
- return 33
- }
- function realTimeStderrTest() {
- # simulate realtime output on stderr, similar to what you might get from
- # curl --no-clobber -L -A "${userAgent}" "${url}" -O "${fileName}"
- # but faster and with less dependencies
- local i n=5;
- for(( i=1; i <= n; i++)); do
- sleep 0.5s;
- printf '%s\n' "stderr $i of $n" >&2;
- done
- return 55
- }
- function realTimeColoredStderrTest() {
- # simulate realtime output on stderr, similar to what you might get from
- # curl --no-clobber -L -A "${userAgent}" "${url}" -O "${fileName}"
- # but faster and with less dependencies
- local red="$(tput setaf 1)";
- local green="$(tput setaf 2)";
- local magenta="$(tput setaf 5)";
- local cyan="$(tput setaf 6)"
- local normal="$(tput sgr0)";
- local i n=5;
- for(( i=1; i <= n; i++)); do
- sleep 0.5s;
- printf '%s\n' "stderr $i of $n" >&2;
- printf '%s\n' "${red}stderr${normal} ${green}${i}${normal} of ${magenta}${n}${normal}";
- done
- return 55
- }
- print_read() {
- local LANG=C IFS REPLY;
- while read -r -d ''; do
- printf "${1}\0%s\0" "$REPLY";
- done
- printf "${1}\0%s\0" "$REPLY";
- }
- function wrapper() {
- local code REPLY;
- # TODO: also pass var names for exporting so caller
- # can grab these. for now, just capture locally
- local outstr errstr rc;
- # TODO: set these from passed options. for now, just hard-code
- local printStderr=0 printStdout=0;
- if [[ '-O' == "$1" ]]; then
- echo "printStdout=1"
- printStdout=1;
- shift 1;
- exec 5>&1
- fi
- if [[ '-E' == "$1" ]]; then
- echo "PrintStderr=1"
- printStderr=1
- shift 1;
- exec 6>&2
- fi
- echo ""
- echo "======================================="
- echo "realtime output"
- echo "======================================="
- {
- while IFS= read -r -d '' code; do
- IFS= read -r -d ''
- case "$code" in
- '1') outstr+="$REPLY"
- ;;
- '2') errstr+="$REPLY"
- ;;
- 'e') rc="$REPLY" ;;
- *)
- #printf '%s\n' 'A dev broke this script' 1>&2
- exit 1
- ;;
- esac
- done
- } < <(
- {
- "${@}" 1> >(
- if [[ 1 == "$printStdout" ]]; then
- tee >(cat - >&5) | print_read '1'
- else
- print_read '1'
- fi
- )
- printf "e\0%d\0" "$?"
- } 2> >(
- if [[ 1 == "$printStderr" ]]; then
- tee >(cat - >&6) | print_read '2'
- else
- print_read '2'
- fi
- )
- )
- echo ""
- echo "======================================="
- echo "captured variables"
- echo "======================================="
- echo "rc: ${rc}";
- echo "errstr: ";
- echo "${errstr}";
- echo ""
- echo "outstr: ";
- echo "${outstr}";
- echo ""
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement