Created
October 30, 2013 01:45
-
-
Save RWJMurphy/7225971 to your computer and use it in GitHub Desktop.
bash scripting helpers -- command line argument parsing, and some logging functions
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
| function usage() { | |
| cat <<EOD | |
| $0 [-hnv] | |
| -h, --help Print this text and exit | |
| -n, --dry-run Dryrun; do not make any changes | |
| -v, --verbose Enable verbose output; repeat for more verbosity | |
| EOD | |
| } | |
| function debug() { | |
| [ $DEBUG != 0 ] && echo $* | |
| } | |
| function info() { | |
| echo $* | |
| } | |
| function warn() { | |
| echo $* 2>&1 | |
| } | |
| function fatal() { | |
| warn $* | |
| exit 1 | |
| } | |
| DEBUG=0 | |
| DRYRUN=0 | |
| while [[ $# > 0 ]]; do | |
| case $1 in | |
| -v|--verbose) | |
| if [ $DEBUG == 0 ]; then | |
| DEBUG=1; | |
| elif [ $DEBUG == 1 ]; then | |
| DEBUG=2 | |
| set -v | |
| else | |
| DEBUG=3 | |
| set -x | |
| fi | |
| ;; | |
| -n|--dry-run) | |
| DRYRUN=1 | |
| ;; | |
| -h|--help) | |
| usage | |
| exit 0 | |
| ;; | |
| *) | |
| warn Unknown flag $1 | |
| usage | |
| exit 1 | |
| ;; | |
| esac | |
| shift | |
| done | |
| [ $DRYRUN != 0 ] && warn "DRYRUN - no changes will be made" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment