Skip to content

Instantly share code, notes, and snippets.

@mlafeldt
Created January 16, 2012 11:47
Show Gist options
  • Save mlafeldt/1620505 to your computer and use it in GitHub Desktop.
Save mlafeldt/1620505 to your computer and use it in GitHub Desktop.
[Shell] --verbose mode based on redirections (avoids if/else code cluttering)
#!/bin/sh
verbose=
case "$1" in
-v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
verbose=1
shift ;;
esac
if [ "$verbose" = 1 ]; then
exec 4>&2 3>&1
else
exec 4>/dev/null 3>/dev/null
fi
to_stdout() {
echo "This goes to stdout."
}
to_stderr() {
echo >&2 "This goes to stderr."
}
to_stdout >&3 2>&4
to_stderr >&3 2>&4
echo "This message is always shown."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment