Advertisement
Guest User

WIP wrapper function for capturing stderr/stdout/rc to vars + optionally printing stdout/stderr

a guest
Jul 4th, 2024
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.49 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. function simpleTest() {
  4.     printf '%s\n' 'This should appear on stderr' >&2;
  5.     printf '%s\n' 'This should appear on stdout';
  6.     return 42
  7. }
  8.  
  9. function simpleColorTest() {
  10.     local red="$(tput setaf 1)";
  11.     local green="$(tput setaf 2)";
  12.     local magenta="$(tput setaf 5)";
  13.     local cyan="$(tput setaf 6)"
  14.     local normal="$(tput sgr0)";
  15.     printf '%s\n' "${red}ERROR:${normal} This should appear on ${magenta}stderr${normal}" >&2;
  16.     printf '%s\n' "${green}PASS:${normal} This should appear on ${cyan}stdout${normal}";
  17.     return 42
  18. }
  19.  
  20. function realTimeStdoutTest() {
  21.     # simulate realtime output on stdout, similar to what you might get from
  22.     #   traceroute reddit.com
  23.     # but faster and with less dependencies
  24.     local i n=5;
  25.     for(( i=1; i <= n; i++)); do
  26.         sleep 0.5s;
  27.         printf '%s\n' "stdout $i of $n";
  28.     done
  29.     return 33
  30. }
  31.  
  32. function realTimeColoredStdoutTest() {
  33.     # simulate realtime output on stdout, similar to what you might get from
  34.     #   traceroute reddit.com
  35.     # but faster and with less dependencies
  36.     local red="$(tput setaf 1)";
  37.     local green="$(tput setaf 2)";
  38.     local magenta="$(tput setaf 5)";
  39.     local cyan="$(tput setaf 6)"
  40.     local normal="$(tput sgr0)";
  41.     local i n=5;
  42.     for(( i=1; i <= n; i++)); do
  43.         sleep 0.5s;
  44.         printf '%s\n' "${cyan}stdout${normal} ${green}${i}${normal} of ${magenta}${n}${normal}";
  45.     done
  46.     return 33
  47. }
  48.  
  49. function realTimeStderrTest() {
  50.     # simulate realtime output on stderr, similar to what you might get from
  51.     #   curl --no-clobber -L -A "${userAgent}" "${url}" -O "${fileName}"
  52.     # but faster and with less dependencies
  53.     local i n=5;
  54.     for(( i=1; i <= n; i++)); do
  55.         sleep 0.5s;
  56.         printf '%s\n' "stderr $i of $n" >&2;
  57.     done
  58.     return 55
  59. }
  60.  
  61. function realTimeColoredStderrTest() {
  62.     # simulate realtime output on stderr, similar to what you might get from
  63.     #   curl --no-clobber -L -A "${userAgent}" "${url}" -O "${fileName}"
  64.     # but faster and with less dependencies
  65.     local red="$(tput setaf 1)";
  66.     local green="$(tput setaf 2)";
  67.     local magenta="$(tput setaf 5)";
  68.     local cyan="$(tput setaf 6)"
  69.     local normal="$(tput sgr0)";
  70.     local i n=5;
  71.     for(( i=1; i <= n; i++)); do
  72.         sleep 0.5s;
  73.         printf '%s\n' "stderr $i of $n" >&2;
  74.         printf '%s\n' "${red}stderr${normal} ${green}${i}${normal} of ${magenta}${n}${normal}";
  75.     done
  76.     return 55
  77. }
  78.  
  79. print_read() {
  80.     local LANG=C IFS REPLY;
  81.     while read -r -d ''; do
  82.         printf "${1}\0%s\0" "$REPLY";
  83.     done
  84.     printf "${1}\0%s\0" "$REPLY";
  85. }
  86.  
  87. function wrapper() {
  88.     local code REPLY;
  89.     # TODO: also pass var names for exporting so caller
  90.     # can grab these. for now, just capture locally
  91.     local outstr errstr rc;
  92.  
  93.     # TODO: set these from passed options. for now, just hard-code
  94.     local printStderr=0 printStdout=0;
  95.  
  96.     if [[ '-O' == "$1" ]]; then
  97.         echo "printStdout=1"
  98.         printStdout=1;
  99.         shift 1;
  100.         exec 5>&1
  101.     fi
  102.     if [[ '-E' == "$1" ]]; then
  103.         echo "PrintStderr=1"
  104.         printStderr=1
  105.         shift 1;
  106.         exec 6>&2
  107.     fi
  108.  
  109.     echo ""
  110.     echo "======================================="
  111.     echo "realtime output"
  112.     echo "======================================="
  113.  
  114.     {
  115.         while IFS= read -r -d '' code; do
  116.             IFS= read -r -d ''
  117.             case "$code" in
  118.                 '1') outstr+="$REPLY"
  119.                 ;;
  120.                 '2') errstr+="$REPLY"
  121.                 ;;
  122.                 'e') rc="$REPLY" ;;
  123.                 *)
  124.                     #printf '%s\n' 'A dev broke this script' 1>&2
  125.                     exit 1
  126.                 ;;
  127.             esac
  128.         done
  129.     } < <(
  130.         {
  131.             "${@}" 1> >(
  132.                 if [[ 1 == "$printStdout" ]]; then
  133.                     tee >(cat - >&5) | print_read '1'
  134.                 else
  135.                     print_read '1'
  136.                 fi
  137.             )
  138.             printf "e\0%d\0" "$?"
  139.         } 2> >(
  140.             if [[ 1 == "$printStderr" ]]; then
  141.                 tee >(cat - >&6) | print_read '2'
  142.             else
  143.                 print_read '2'
  144.             fi
  145.         )
  146.     )
  147.  
  148.     echo ""
  149.     echo "======================================="
  150.     echo "captured variables"
  151.     echo "======================================="
  152.     echo "rc: ${rc}";
  153.     echo "errstr: ";
  154.     echo "${errstr}";
  155.     echo ""
  156.     echo "outstr: ";
  157.     echo "${outstr}";
  158.     echo ""
  159. }
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement