Merge branch 'master' of github.com:kvz/bash3boilerplate

Conflicts:
	main.sh
This commit is contained in:
Kevin van Zonneveld 2014-07-23 11:34:05 +02:00
commit e2602f6a58
2 changed files with 32 additions and 22 deletions

View File

@ -1,3 +1,13 @@
# bash3boilerplate
<!-- badges/ -->
[![Build Status](https://secure.travis-ci.org/kvz/bash3boilerplate.png?branch=master)](http://travis-ci.org/kvz/bash3boilerplate "Check this project's build status on TravisCI")
[![Gittip donate button](http://img.shields.io/gittip/kvz.png)](https://www.gittip.com/kvz/ "Sponsor the development of bash3boilerplate via Gittip")
[![Flattr donate button](http://img.shields.io/flattr/donate.png?color=yellow)](https://flattr.com/submit/auto?user_id=kvz&url=https://github.com/kvz/bash3boilerplate&title=bash3boilerplate&language=&tags=github&category=software "Sponsor the development of bash3boilerplate via Flattr")
[![PayPayl donate button](http://img.shields.io/paypal/donate.png?color=yellow)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=kevin%40vanzonneveld%2enet&lc=NL&item_name=Open%20source%20donation%20to%20Kevin%20van%20Zonneveld&currency_code=USD&bn=PP-DonationsBF%3abtn_donate_SM%2egif%3aNonHosted "Sponsor the development of bash3boilerplate via Paypal")
[![BitCoin donate button](http://img.shields.io/bitcoin/donate.png?color=yellow)](https://coinbase.com/checkouts/19BtCjLCboRgTAXiaEvnvkdoRyjd843Dg2 "Sponsor the development of bash3boilerplate via BitCoin")
<!-- /badges -->
When hacking up BASH scripts, I often find there are some When hacking up BASH scripts, I often find there are some
higherlevel things like logging, configuration, commandline argument higherlevel things like logging, configuration, commandline argument
parsing that: parsing that:
@ -12,15 +22,9 @@ me) programs.
An up to date [intro is found on my blog](http://kvz.io/blog/2013/02/26/introducing-bash3boilerplate/). An up to date [intro is found on my blog](http://kvz.io/blog/2013/02/26/introducing-bash3boilerplate/).
## Contributing
Feel free to fork this project. We will happily merge bug fixes or other small
improvements. For bigger changes you should probably get in touch with us
before you start to avoid not seeing them merged.
## Versioning ## Versioning
Discourse implements the Semantic Versioning guidelines. This project implements the Semantic Versioning guidelines.
Releases will be numbered with the following format: Releases will be numbered with the following format:
@ -32,4 +36,10 @@ And constructed with the following guidelines:
* New additions without breaking backward compatibility bumps the minor (and resets the patch) * New additions without breaking backward compatibility bumps the minor (and resets the patch)
* Bug fixes and misc changes bumps the patch * Bug fixes and misc changes bumps the patch
For more information on SemVer, please visit http://semver.org/.
For more information on SemVer, please visit [http://semver.org](http://semver.org).
## License
Copyright (c) 2013 Kevin van Zonneveld, [http://kvz.io](http://kvz.io)
Licensed under MIT: [http://kvz.io/licenses/LICENSE-MIT](http://kvz.io/licenses/LICENSE-MIT)

28
main.sh
View File

@ -15,7 +15,7 @@
# - Kevin van Zonneveld (http://kvz.io) # - Kevin van Zonneveld (http://kvz.io)
# #
# Usage: # Usage:
# LOG_LEVEL=7 ./main.sh first_arg second_arg # LOG_LEVEL=7 ./main.sh -f /tmp/x -d
# #
# Licensed under MIT # Licensed under MIT
# Copyright (c) 2013 Kevin van Zonneveld (http://kvz.io) # Copyright (c) 2013 Kevin van Zonneveld (http://kvz.io)
@ -30,8 +30,8 @@ LOG_LEVEL="${LOG_LEVEL:-6}" # 7 = debug -> 0 = emergency
# Commandline options. This defines the usage page, and is used to parse cli # Commandline options. This defines the usage page, and is used to parse cli
# opts & defaults from. The parsing is unforgiving so be precise in your syntax # opts & defaults from. The parsing is unforgiving so be precise in your syntax
read -r -d '' usage <<-'EOF' read -r -d '' usage <<-'EOF'
-f [arg] Filename to process. -f [arg] Filename to process. Required.
-t [arg] Location of tempfile. Default="/tmp/x" -t [arg] Location of tempfile. Default="/tmp/bar"
-d Enables debug mode -d Enables debug mode
-h This page -h This page
EOF EOF
@ -69,13 +69,12 @@ function notice () { [ "${LOG_LEVEL}" -ge 5 ] && echo "$(_fmt notice) ${@}" 1
function info () { [ "${LOG_LEVEL}" -ge 6 ] && echo "$(_fmt info) ${@}" 1>&2 || true; } function info () { [ "${LOG_LEVEL}" -ge 6 ] && echo "$(_fmt info) ${@}" 1>&2 || true; }
function debug () { [ "${LOG_LEVEL}" -ge 7 ] && echo "$(_fmt debug) ${@}" 1>&2 || true; } function debug () { [ "${LOG_LEVEL}" -ge 7 ] && echo "$(_fmt debug) ${@}" 1>&2 || true; }
function help () { function help () {
echo "" echo "" 1>&2
echo " ${@}" echo " ${@}" 1>&2
echo "" echo "" 1>&2
echo " ${usage}" echo " ${usage}" 1>&2
echo "" echo "" 1>&2
exit 1 exit 1
} }
@ -134,7 +133,7 @@ shift $((OPTIND-1))
[ "$1" = "--" ] && shift [ "$1" = "--" ] && shift
### Switches ### Switches (like -d for debugmdoe, -h for showing helppage)
##################################################################### #####################################################################
# debug mode # debug mode
@ -145,22 +144,23 @@ fi
# help mode # help mode
if [ "${arg_h}" = "1" ]; then if [ "${arg_h}" = "1" ]; then
# Help exists with code 1
help "Help using ${0}" help "Help using ${0}"
fi fi
### Validation ### Validation (decide what's required for running your script and error out)
##################################################################### #####################################################################
[ -z "${arg_f}" ] && help "Setting a filename with -f is required" [ -z "${arg_f}" ] && help "Setting a filename with -f is required"
[ -z "${LOG_LEVEL}" ] && emergency "Cannot continue without loglevel. " [ -z "${LOG_LEVEL}" ] && emergency "Cannot continue without LOG_LEVEL. "
### Runtime ### Runtime
##################################################################### #####################################################################
# Exit on error. Append ||true if you expect an error. # Exit on error. Append ||true if you expect an error.
# set -e is safer than #!/bin/bash -e because that is nutralised if # set -e is safer than #!/bin/bash -e because that is neutralised if
# someone runs your script like `bash yourscript.sh` # someone runs your script like `bash yourscript.sh`
set -o errexit set -o errexit
set -o nounset set -o nounset