-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathshellcheck.sh
executable file
·92 lines (85 loc) · 2.46 KB
/
shellcheck.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
set -e
set -u
set -o pipefail
#
# Disable some shellcheck warnings:
# * SC2004: $/${} is unnecessary on arithmetic variables.
# But for consistency we prefer to always use ${} anyway.
# * SC2015: Note that A && B || C is not if-then-else. C may run when A is true.
# We know and use this construct regularly to create "transactions"
# where C is only executed when both A and B have succeeded.
#
export SHELLCHECK_OPTS="-e SC2004 -e SC2015"
function showHelp() {
#
# Display commandline help on STDOUT.
#
cat <<EOH
===============================================================================================================
Script for sanity checking of Bash code of this repo using ShellCheck.
Usage:
$(basename "${0}") OPTIONS
Options:
-h Show this help.
-v Enables verbose output.
===============================================================================================================
EOH
exit 0
}
#
# Parse commandline options
#
declare format='gcc' # default
while getopts ":hv" opt
do
case "${opt}" in
h)
showHelp
;;
v)
format='tty'
;;
\?)
printf '%s\n' "FATAL: Invalid option -${OPTARG}. Try $(basename "${0}") -h for help."
exit 1
;;
:)
printf '%s\n' "FATAL: Option -${OPTARG} requires an argument. Try $(basename "${0}") -h for help."
exit 1
;;
*)
printf '%s\n' "FATAL Unhandled option. Try $(basename "${0}") -h for help."
exit 1
;;
esac
done
#
# Check if ShellCheck is installed.
#
which shellcheck 2>&1 >/dev/null \
|| {
printf '%s\n' 'FATAL: cannot find shellcheck; make sure it is installed and can be found in ${PATH}.'
exit 1
}
MYDIR="$(cd -P "$(dirname "${0}")" && pwd)"
#
# Run ShellCheck for all Bash scripts in the bin/ subdir.
# * Includes sourced files, so the libraries from the lib/ subfolder
# are checked too as long a they are used in at least one script.
#
CIRCLECI=true
if [[ "${CIRCLECI:-}" == true ]]
then
#
# Exclude SC2154 (warning for variables that are referenced but not assigned),
# because we cannot easily resolve variables sourced from etc/*.cfg config files.
#
export SHELLCHECK_OPTS="${SHELLCHECK_OPTS} -e SC2154"
#
# Exclude SC2312 (warning for masking return values of command in a subshell when using process substitution) temporarily,
# because we did not find a reliable fix yet....
#
export SHELLCHECK_OPTS="${SHELLCHECK_OPTS} -e SC2312"
fi
shellcheck -a -x -o all -f "${format}" "${MYDIR}"/../bin/*.sh | sed "s|${MYDIR}/../||g"