Skip to content

Instantly share code, notes, and snippets.

@kusalananda
Last active February 24, 2017 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kusalananda/1981b54a148db161ec166409ad22d599 to your computer and use it in GitHub Desktop.
Save kusalananda/1981b54a148db161ec166409ad22d599 to your computer and use it in GitHub Desktop.
Shell function that test a command in all installed shells and captures+displays the output. Should work with both ksh93 and bash.
function shtest
{
# Will run the given command in all avaliable shells and capture and
# display the output generated on standard output as well as any
# diagnostic messages generated on standard error, together with the
# exit code.
grep '^[^#]' /etc/shells | (
typeset -a tmpfiles
trap 'rm -f "${tmpfiles[@]}"' EXIT
while read realshell; do
typeset shell="${realshell##*/}"
typeset tmpout="$( mktemp -p /tmp "$shell-out.XXXXXXXX" )"
typeset tmperr="$( mktemp -p /tmp "$shell-err.XXXXXXXX" )"
tmpfiles+=( "$tmpout" "$tmperr" )
command "$realshell" -c "$@" 1>"$tmpout" 2>"$tmperr"
err="$?"
printf '=== %s (exit %d)\n' "$shell" "$err"
echo "--- stdout:"
cat "$tmpout"
echo "--- stderr:"
cat "$tmperr"
done )
}
# vim: ft=sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment