Skip to content

Commit 672b422

Browse files
committed
Address shellcheck issues in use-case dir
1 parent f31b1a5 commit 672b422

File tree

6 files changed

+52
-40
lines changed

6 files changed

+52
-40
lines changed

prerequisites/use-case/bootstrap.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
#
66
# - Exports bash3boilerplate features and variables to the invoking script
77
# - Invokes functions containing commands extracted from the bash3boilerplate
8-
# main.sh as part of a refactoring to facilitate wholesale reuse of main.sh's
8+
# main.sh as part of a refactoring to facilitate wholesale reuse of main.sh's
99
# contents of without modification.
1010
#
11-
# Usage (as invoked in my-script.sh):
11+
# Usage (as invoked in my-script.sh):
1212
#
1313
# source bootstrap.sh "${@}"
1414
#
@@ -29,8 +29,13 @@
2929
# Licensed under MIT
3030
# Copyright (c) 2013 Kevin van Zonneveld (http://kvz.io)
3131

32+
# shellcheck source=./set_environment_and_color.sh
3233
source "${B3B_USE_CASE}"/set_environment_and_color.sh # turn on errexit, nounset, pipefail, default log level
34+
# shellcheck source=./set_magic_variables.sh
3335
source "${B3B_USE_CASE}"/set_magic_variables.sh "$(caller 0)" # set __dir, __file, __filename, __base, __os
36+
# shellcheck source=./define_functions.sh
3437
source "${B3B_USE_CASE}"/define_functions.sh # help/usage function and debug/info output functions
38+
# shellcheck source=./parse_command_line.sh
3539
source "${B3B_USE_CASE}"/parse_command_line.sh "${@:-}" # parse the command line
40+
# shellcheck source=./set_common_switches.sh
3641
source "${B3B_USE_CASE}"/set_common_switches.sh # provide defaults for -h, -V, and -d

prerequisites/use-case/define_functions.sh

100755100644
Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
#!/usr/bin/env bash
21
# BASH3 Boilerplate
32
#
43
# define_functions.sh
54
#
6-
# - Defines helper functions containing commands extracted from the
7-
# bash3boilerplate main.sh as part of a refactoring to facilitate
5+
# - Defines helper functions containing commands extracted from the
6+
# bash3boilerplate main.sh as part of a refactoring to facilitate
87
# wholesale reuse of main.sh's contents of without modification.
98
#
10-
# Usage (as invoked in bootstrap.sh):
9+
# Usage (as invoked in bootstrap.sh):
1110
#
1211
# source define_functions.sh
1312
#
@@ -32,6 +31,7 @@
3231
### Functions
3332
#####################################################################
3433

34+
# shellcheck disable=SC2034
3535
function _fmt () {
3636
local color_debug="\x1b[35m"
3737
local color_info="\x1b[32m"
@@ -49,41 +49,44 @@ function _fmt () {
4949
# Don't use colors on pipes or non-recognized terminals
5050
color=""; color_reset=""
5151
fi
52-
echo -e "$(date -u +"%Y-%m-%d %H:%M:%S UTC") ${color}$(printf "[%9s]" ${1})${color_reset}";
52+
echo -e "$(date -u +"%Y-%m-%d %H:%M:%S UTC") ${color}$(printf "[%9s]" "${1}")${color_reset}";
5353
}
5454

55-
# The block of single-line functions below all print to STDERR,
55+
# The block of single-line functions below all print to STDERR,
5656
# leaving STDOUT for piping machine readable information to other
57-
# software. Above each such function is a commented demonstration
58-
# of its usage. Execution continues after an invocation of each
59-
# function, except the "emergency" function, which causes
57+
# software. Above each such function is a commented demonstration
58+
# of its usage. Execution continues after an invocation of each
59+
# function, except the "emergency" function, which causes
6060
# termination with a non-zero exit status.
6161

62+
# shellcheck disable=SC2015
63+
{
6264
# emergency "A \"panic\" condition usually affecting multiple apps/servers/sites. At this level it would usually notify all tech staff on call."
63-
function emergency () { echo "$(_fmt emergency) ${@}" 1>&2 || true; exit 1; }
65+
function emergency () { echo "$(_fmt emergency) ${*}" 1>&2 || true; exit 1; }
6466
# alert "Should be corrected immediately, therefore notify staff who can fix the problem. An example would be the loss of a primary ISP connection."
65-
function alert () { [ "${LOG_LEVEL}" -ge 1 ] && echo "$(_fmt alert) ${@}" 1>&2 || true; }
67+
function alert () { [ "${LOG_LEVEL}" -ge 1 ] && echo "$(_fmt alert) ${*}" 1>&2 || true; }
6668
# critical "Should be corrected immediately, but indicates failure in a primary system, an example is a loss of a backup ISP connection."
67-
function critical () { [ "${LOG_LEVEL}" -ge 2 ] && echo "$(_fmt critical) ${@}" 1>&2 || true; }
69+
function critical () { [ "${LOG_LEVEL}" -ge 2 ] && echo "$(_fmt critical) ${*}" 1>&2 || true; }
6870
# error "Non-urgent failures, these should be relayed to developers or admins; each item must be resolved within a given time."
69-
function error () { [ "${LOG_LEVEL}" -ge 3 ] && echo "$(_fmt error) ${@}" 1>&2 || true; }
71+
function error () { [ "${LOG_LEVEL}" -ge 3 ] && echo "$(_fmt error) ${*}" 1>&2 || true; }
7072
# 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"
71-
function warning () { [ "${LOG_LEVEL}" -ge 4 ] && echo "$(_fmt warning) ${@}" 1>&2 || true; }
73+
function warning () { [ "${LOG_LEVEL}" -ge 4 ] && echo "$(_fmt warning) ${*}" 1>&2 || true; }
7274
# 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."
73-
function notice () { [ "${LOG_LEVEL}" -ge 5 ] && echo "$(_fmt notice) ${@}" 1>&2 || true; }
75+
function notice () { [ "${LOG_LEVEL}" -ge 5 ] && echo "$(_fmt notice) ${*}" 1>&2 || true; }
7476
# info "Normal operational messages - may be harvested for reporting, measuring throughput, etc. - no action required."
75-
function info () { [ "${LOG_LEVEL}" -ge 6 ] && echo "$(_fmt info) ${@}" 1>&2 || true; }
77+
function info () { [ "${LOG_LEVEL}" -ge 6 ] && echo "$(_fmt info) ${*}" 1>&2 || true; }
7678
# debug "Info useful to developers for debugging the application, not useful during operations."
77-
function debug () { [ "${LOG_LEVEL}" -ge 7 ] && echo "$(_fmt debug) ${@}" 1>&2 || true; }
78-
79+
function debug () { [ "${LOG_LEVEL}" -ge 7 ] && echo "$(_fmt debug) ${*}" 1>&2 || true; }
80+
}
7981
function suppress_debug_messages() { export LOG_LEVEL=6; }
8082
function suppress_info_debug_messages () { export LOG_LEVEL=5; }
8183
function suppress_notice_info_debug_messages () { export LOG_LEVEL=4; }
8284

8385
function help () {
8486
echo "" 1>&2
85-
echo " ${@}" 1>&2
87+
echo " ${*}" 1>&2
8688
echo "" 1>&2
89+
# shellcheck disable=SC2154
8790
cat "${__usage}" 1>&2
8891
echo "" 1>&2
8992
exit 1

prerequisites/use-case/parse_command_line.sh

100755100644
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
#!/usr/bin/env bash
21
# BASH3 Boilerplate
32
#
43
# parse_command_line.sh
54
#
65
# - Uses usage information defined in "${__usage}" to parse the command line.
76
# - Defines a function containing commands extracted from the bash3boilerplate
8-
# main.sh as part of a refactoring to facilitate wholesale reuse of main.sh's
7+
# main.sh as part of a refactoring to facilitate wholesale reuse of main.sh's
98
# contents of without modification.
109
#
1110
# More info:
@@ -25,7 +24,7 @@
2524
# Usage (as invoked in bootstraph.sh):
2625
#
2726
# source parse_command_line.sh
28-
# parse_command_line ${@:2}
27+
# parse_command_line ${@:2}
2928
#
3029
# Licensed under MIT
3130
# Copyright (c) 2013 Kevin van Zonneveld (http://kvz.io)
@@ -36,13 +35,14 @@ function parse_command_line(){
3635
# source this script and pass $@ as the argument
3736

3837
# Translate usage string -> getopts arguments, and set $arg_<flag> defaults
39-
while read line; do
38+
# shellcheck disable=SC2154
39+
while read -r line; do
4040
# fetch single character version of option string
4141
opt="$(echo "${line}" |awk '{print $1}' |sed -e 's#^-##')"
4242

4343
# fetch long version if present
4444
long_opt="$(echo "${line}" |awk '/\-\-/ {print $2}' |sed -e 's#^--##')"
45-
long_opt_mangled="$(sed 's#-#_#g' <<< $long_opt)"
45+
long_opt_mangled="$(sed 's#-#_#g' <<< "$long_opt")"
4646

4747
# map long name back to short name
4848
varname="short_opt_${long_opt_mangled}"
@@ -69,6 +69,7 @@ while read line; do
6969
fi
7070
done < "${__usage}"
7171

72+
7273
# Allow long options like --this
7374
opts="${opts}-:"
7475

@@ -80,21 +81,21 @@ set +o nounset # unexpected arguments will cause unbound variables
8081
# to be dereferenced
8182
# Overwrite $arg_<flag> defaults with the actual CLI options
8283
while getopts "${opts}" opt; do
83-
[ "${opt}" = "?" ] && help "Invalid use of script: ${@} "
84+
[ "${opt}" = "?" ] && help "Invalid use of script: ${*} "
8485

8586
if [ "${opt}" = "-" ]; then
8687
# OPTARG is long-option-name or long-option=value
8788
if [[ "${OPTARG}" =~ .*=.* ]]; then
8889
# --key=value format
8990
long=${OPTARG/=*/}
90-
long_mangled="$(sed 's#-#_#g' <<< $long)"
91+
long_mangled="$(sed 's#-#_#g' <<< "$long")"
9192
# Set opt to the short option corresponding to the long option
9293
eval "opt=\"\${short_opt_${long_mangled}}\""
9394
OPTARG=${OPTARG#*=}
9495
else
9596
# --key value format
9697
# Map long name to short version of option
97-
long_mangled="$(sed 's#-#_#g' <<< $OPTARG)"
98+
long_mangled="$(sed 's#-#_#g' <<< "$OPTARG")"
9899
eval "opt=\"\${short_opt_${long_mangled}}\""
99100
# Only assign OPTARG if option takes an argument
100101
eval "OPTARG=\"\${@:OPTIND:\${has_arg_${opt}}}\""
@@ -119,6 +120,7 @@ set -o nounset # no more unbound variable references expected
119120

120121
shift $((OPTIND-1))
121122

123+
# shellcheck disable=SC2015
122124
[ "${1:-}" = "--" ] && shift || true
123125
}
124126
export -f parse_command_line # make function available to subshells

prerequisites/use-case/set_common_switches.sh

100755100644
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env bash
21
# BASH3 Boilerplate
32
#
43
# set_common_switches.sh
@@ -31,6 +30,8 @@
3130
### Switches (like -d for debugmode, -h for showing helppage)
3231
#####################################################################
3332

33+
# shellcheck disable=SC2154
34+
{
3435
# debug mode
3536
if [ "${arg_d}" = "1" ]; then
3637
set -o xtrace
@@ -47,3 +48,4 @@ if [ "${arg_h}" = "1" ]; then
4748
# Help exists with code 1
4849
help "Help using ${0}"
4950
fi
51+
}

prerequisites/use-case/set_environment_and_color.sh

100755100644
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
#!/usr/bin/env bash
21
# BASH3 Boilerplate
32
#
43
# set_environment_and_color.sh
54
#
65
# - Sets variables that control the behavior of the invoking script.
76
# - Contains commands extracted from the bash3boilerplate main.sh as
8-
# part of a refactoring to facilitate wholesale reuse of main.sh's
7+
# part of a refactoring to facilitate wholesale reuse of main.sh's
98
# contents of without modification.
109
#
11-
# Usage (as invoked in bootstrap.sh):
10+
# Usage (as invoked in bootstrap.sh):
1211
#
13-
# source set_environment_and_color.sh
12+
# source set_environment_and_color.sh
1413
#
1514
# More info:
1615
#

prerequisites/use-case/set_magic_variables.sh

100755100644
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
#!/usr/bin/env bash
21
# BASH3 Boilerplate
32
#
43
# set_magic_variables.sh
54
#
65
# - Sets the variables __dir, __file, __filename, __base, and __os
76
# - Defines a function containing commands extracted from the bash3boilerplate
8-
# main.sh as part of a refactoring to facilitate wholesale reuse of main.sh's
7+
# main.sh as part of a refactoring to facilitate wholesale reuse of main.sh's
98
# contents of without modification.
109
#
1110
# Usage (as invoked in bootstrap.sh):
@@ -28,14 +27,16 @@
2827
#
2928
# Licensed under MIT
3029
# Copyright (c) 2013 Kevin van Zonneveld (http://kvz.io)
31-
32-
[ -z "${1}" ] && echo 'Usage: source set_magic_variables.sh "$(caller 0)"'
30+
31+
# shellcheck disable=SC2016
32+
[ -z "${1}" ] && echo 'Usage: source set_magic_variables.sh "$(caller 0)"'
33+
# shellcheck disable=SC2034
3334
function set_magic_variables(){
34-
text_after_final_space="${1##* }"
35+
text_after_final_space="${1##* }"
3536
__dir="$(cd "$(dirname "${text_after_final_space}")" && pwd)"
3637
__file="${__dir}/$(basename "${text_after_final_space}")"
3738
__filename="$(basename "${text_after_final_space}")"
38-
__base="$(basename ${__file} .sh)"
39+
__base="$(basename "${__file}" .sh)"
3940
__os="Linux"
4041
if [[ "${OSTYPE:-}" == "darwin"* ]]; then
4142
__os="OSX"

0 commit comments

Comments
 (0)