Created
January 16, 2012 11:47
-
-
Save mlafeldt/1620505 to your computer and use it in GitHub Desktop.
[Shell] --verbose mode based on redirections (avoids if/else code cluttering)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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