Quantcast
Channel: Is there a simple test for anything printed on stderr in shell/Bash? - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 4

Answer by Notiz Me for Is there a simple test for anything printed on stderr in shell/Bash?

$
0
0

The most voted answer will work in most cases. But since I use set +o errexit in bash it errored out. This should work better in bash:

fail_if_stderr() (  # save current options  bash_options="${-}"  # disable exit on error  set +o errexit  # Save return code of command in rc  rc=$({    ("$@" 2>&1 >&3 3>&- 4>&-; echo "$?" >&4) |    grep '^'>&2 3>&- 4>&-  } 4>&1)  # Save return code of grep in err_in_stderr  err_in_stderr=$?  # enable exit on error if it was previously enabled  test "${bash_options#*e*}" != "$bash_options" && set -o errexit  # exit with original return code if it's not zero  [ "$rc" -eq 0 ] || exit "$rc"  # exit with return code 125 if something was in stderr  [ "$err_in_stderr" -ne 0 ] || exit 125) 3>&1

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>