Skip to content

Instantly share code, notes, and snippets.

@RWJMurphy
Created October 30, 2013 01:45
Show Gist options
  • Select an option

  • Save RWJMurphy/7225971 to your computer and use it in GitHub Desktop.

Select an option

Save RWJMurphy/7225971 to your computer and use it in GitHub Desktop.
bash scripting helpers -- command line argument parsing, and some logging functions
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