2016-11-09 09:03:00 +00:00
#!/usr/bin/env bash
# This file:
#
# - Demos BASH3 Boilerplate (change this for your script)
#
# Usage:
#
# LOG_LEVEL=7 ./example.sh -f /tmp/x -d (change this for your script)
#
2017-02-10 19:08:51 +00:00
# Based on a template by BASH3 Boilerplate v2.3.0
2016-11-09 09:03:00 +00:00
# http://bash3boilerplate.sh/#authors
#
# The MIT License (MIT)
# Copyright (c) 2013 Kevin van Zonneveld and contributors
# You are not obligated to bundle the LICENSE file with your b3bp projects as long
# as you leave these references intact in the header comments of your source files.
### BASH3 Boilerplate (b3bp) Header
##############################################################################
# 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
# - A short option must be preset for every long option; but every short option
# need not have a long option
# - `--` is respected as the separator between options and arguments
# - We do not bash-expand defaults, so setting '~/app' as a default will not resolve to ${HOME}.
# you can use bash variables to work around this (so use ${HOME} instead)
2018-01-21 22:02:03 +00:00
# shellcheck disable=SC2034
2016-11-09 09:03:00 +00:00
read -r -d '' __usage <<-'EOF' || true # exits non-zero when EOF encountered
-f --file [ arg] Filename to process. Required.
-t --temp [ arg] Location of tempfile. Default = "/tmp/bar"
-v Enable verbose mode, print script as it is executed
-d --debug Enables debug mode
-h --help This page
-n --no-color Disable color output
-1 --one Do just one thing
2019-09-20 09:40:06 +00:00
-i --input [ arg] File to process. Can be repeated.
-x Specify a flag. Can be repeated.
2016-11-09 09:03:00 +00:00
EOF
2018-01-21 22:02:03 +00:00
# shellcheck disable=SC2034
2016-11-09 09:03:00 +00:00
read -r -d '' __helptext <<-'EOF' || true # exits non-zero when EOF encountered
This is Bash3 Boilerplate' s help text. Feel free to add any description of your
program or elaborate more on command-line arguments. This section is not
parsed and will be added as-is to the help.
EOF
2016-12-16 07:18:03 +00:00
# shellcheck source=main.sh
2016-11-09 09:03:00 +00:00
source " $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " && pwd ) /main.sh "
2017-02-10 19:08:51 +00:00
### Signal trapping and backtracing
##############################################################################
function __b3bp_cleanup_before_exit ( ) {
info "Cleaning up. Done"
}
trap __b3bp_cleanup_before_exit EXIT
# requires `set -o errtrace`
__b3bp_err_report( ) {
local error_code
error_code = ${ ? }
2018-01-21 22:02:03 +00:00
# shellcheck disable=SC2154
2017-02-10 19:08:51 +00:00
error " Error in ${ __file } in function ${ 1 } on line ${ 2 } "
exit ${ error_code }
}
# Uncomment the following line for always providing an error backtrace
# trap '__b3bp_err_report "${FUNCNAME:-.}" ${LINENO}' ERR
2016-11-09 09:03:00 +00:00
### Command-line argument switches (like -d for debugmode, -h for showing helppage)
##############################################################################
# debug mode
2016-12-14 08:54:25 +00:00
if [ [ " ${ arg_d : ? } " = "1" ] ] ; then
2016-11-09 09:03:00 +00:00
set -o xtrace
2019-05-06 08:04:09 +00:00
PS4 = '+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
2016-11-09 09:03:00 +00:00
LOG_LEVEL = "7"
2017-02-10 19:08:51 +00:00
# Enable error backtracing
trap '__b3bp_err_report "${FUNCNAME:-.}" ${LINENO}' ERR
2016-11-09 09:03:00 +00:00
fi
# verbose mode
2016-12-14 08:54:25 +00:00
if [ [ " ${ arg_v : ? } " = "1" ] ] ; then
2016-11-09 09:03:00 +00:00
set -o verbose
fi
# no color mode
2016-12-14 08:54:25 +00:00
if [ [ " ${ arg_n : ? } " = "1" ] ] ; then
2016-11-09 09:03:00 +00:00
NO_COLOR = "true"
fi
# help mode
2016-12-14 08:54:25 +00:00
if [ [ " ${ arg_h : ? } " = "1" ] ] ; then
2016-11-09 09:03:00 +00:00
# Help exists with code 1
help " Help using ${ 0 } "
fi
### Validation. Error out if the things required for your script are not present
##############################################################################
2016-12-14 08:54:25 +00:00
[ [ " ${ arg_f :- } " ] ] || help "Setting a filename with -f or --file is required"
[ [ " ${ LOG_LEVEL :- } " ] ] || emergency "Cannot continue without LOG_LEVEL. "
2016-11-09 09:03:00 +00:00
### Runtime
##############################################################################
2018-01-21 22:02:03 +00:00
# shellcheck disable=SC2154
2017-02-10 19:08:51 +00:00
info " __i_am_main_script: ${ __i_am_main_script } "
2018-01-21 22:02:03 +00:00
# shellcheck disable=SC2154
2016-11-09 09:03:00 +00:00
info " __file: ${ __file } "
2018-01-21 22:02:03 +00:00
# shellcheck disable=SC2154
2016-11-09 09:03:00 +00:00
info " __dir: ${ __dir } "
2018-01-21 22:02:03 +00:00
# shellcheck disable=SC2154
2016-11-09 09:03:00 +00:00
info " __base: ${ __base } "
info " OSTYPE: ${ OSTYPE } "
info " arg_f: ${ arg_f } "
info " arg_d: ${ arg_d } "
info " arg_v: ${ arg_v } "
info " arg_h: ${ arg_h } "
2019-09-20 09:40:06 +00:00
if [ [ -v arg_i ] ]
then
info " arg_i: ${# arg_i [@] } "
for input_file in " ${ arg_i [@] } "
do
info " - ${ input_file } "
done
else
info "arg_i: 0"
fi
# shellcheck disable=SC2015
[ [ -v arg_x ] ] && info " arg_x: ${# arg_x [@] } " || info "arg_x: 0"
2016-11-09 09:03:00 +00:00
2018-01-21 22:02:03 +00:00
info " $( echo -e "multiple lines example - line #1\\nmultiple lines example - line #2\\nimagine logging the output of 'ls -al /path/'" ) "
2016-11-09 09:03:00 +00:00
# All of these go to STDERR, so you can use STDOUT for piping machine readable information to other software
debug "Info useful to developers for debugging the application, not useful during operations."
info "Normal operational messages - may be harvested for reporting, measuring throughput, etc. - no action required."
notice "Events that are unusual but not error conditions - might be summarized in an email to developers or admins to spot potential problems - no immediate action required."
warning "Warning messages, not an error, but indication that an error will occur if action is not taken, e.g. file system 85% full - each item must be resolved within a given time. This is a debug message"
error "Non-urgent failures, these should be relayed to developers or admins; each item must be resolved within a given time."
critical "Should be corrected immediately, but indicates failure in a primary system, an example is a loss of a backup ISP connection."
alert "Should be corrected immediately, therefore notify staff who can fix the problem. An example would be the loss of a primary ISP connection."
emergency "A \"panic\" condition usually affecting multiple apps/servers/sites. At this level it would usually notify all tech staff on call."