mirror of
https://github.com/kvz/bash3boilerplate.git
synced 2025-04-25 05:19:40 +00:00
Small clarifications
This commit is contained in:
parent
bd844228f9
commit
ca5f37bb4d
27
main.sh
27
main.sh
@ -1,27 +1,28 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
#
|
#
|
||||||
# Template to write better bash scripts. More info: http://kvz.io
|
# Template to write better bash scripts. More info: http://kvz.io
|
||||||
# Version 0.0.1
|
# Version 0.0.2
|
||||||
#
|
#
|
||||||
# Usage:
|
# Usage:
|
||||||
# LOG_LEVEL=7 ./template.sh first_arg second_arg
|
# LOG_LEVEL=7 ./template.sh -f /tmp/foo -d
|
||||||
#
|
#
|
||||||
# Licensed under MIT
|
# Licensed under MIT
|
||||||
# Copyright (c) 2013 Kevin van Zonneveld
|
# Copyright (c) 2013 Kevin van Zonneveld
|
||||||
# http://twitter.com/kvz
|
# http://twitter.com/kvz
|
||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
### Configuration
|
### Configuration
|
||||||
#####################################################################
|
#####################################################################
|
||||||
|
|
||||||
# Environment variables
|
# Environment variables
|
||||||
[ -z "${LOG_LEVEL}" ] && LOG_LEVEL="6" # 7 = debug -> 0 = emergency
|
[ -z "${LOG_LEVEL}" ] && LOG_LEVEL="6" # 7 = debug, 0 = emergency
|
||||||
|
|
||||||
# Commandline options. This defines the usage page, and is used to parse cli opts & defaults from.
|
# 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:
|
# 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
|
||||||
@ -46,8 +47,7 @@ function _fmt () {
|
|||||||
color_reset="\x1b[0m"
|
color_reset="\x1b[0m"
|
||||||
if [ "${TERM}" != "xterm" ] || [ -t 1 ]; then
|
if [ "${TERM}" != "xterm" ] || [ -t 1 ]; then
|
||||||
# Don't use colors on pipes or non-recognized terminals
|
# Don't use colors on pipes or non-recognized terminals
|
||||||
color=""
|
color=""; color_reset=""
|
||||||
color_reset=""
|
|
||||||
fi
|
fi
|
||||||
echo -e "$(date -u +"%Y-%m-%d %H:%M:%S UTC") ${color}$(printf "[%9s]" ${1})${color_reset}";
|
echo -e "$(date -u +"%Y-%m-%d %H:%M:%S UTC") ${color}$(printf "[%9s]" ${1})${color_reset}";
|
||||||
}
|
}
|
||||||
@ -124,26 +124,29 @@ shift $((OPTIND-1))
|
|||||||
[ "$1" = "--" ] && shift
|
[ "$1" = "--" ] && shift
|
||||||
|
|
||||||
|
|
||||||
### Switches
|
### Switches (like -d for debugmdoe, -h for showing helppage)
|
||||||
#####################################################################
|
#####################################################################
|
||||||
|
|
||||||
# debug mode
|
# debug mode
|
||||||
if [ "${arg_d}" = "1" ]; then
|
if [ "${arg_d}" = "1" ]; then
|
||||||
|
# turn on tracing
|
||||||
set -x
|
set -x
|
||||||
|
# output debug messages
|
||||||
LOG_LEVEL="7"
|
LOG_LEVEL="7"
|
||||||
fi
|
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
|
||||||
@ -152,7 +155,7 @@ fi
|
|||||||
# 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 nutralised if
|
||||||
# someone runs your script like `bash yourscript.sh`
|
# someone runs your script like `bash yourscript.sh`
|
||||||
set -e
|
set -eu
|
||||||
|
|
||||||
# Bash will remember & return the highest exitcode in a chain of pipes.
|
# Bash will remember & return the highest exitcode in a chain of pipes.
|
||||||
# This way you can catch the error in case mysqldump fails in `mysqldump |gzip`
|
# This way you can catch the error in case mysqldump fails in `mysqldump |gzip`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user