-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck
More file actions
executable file
·93 lines (69 loc) · 1.82 KB
/
check
File metadata and controls
executable file
·93 lines (69 loc) · 1.82 KB
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
93
#! /bin/bash
die() {
echo "$@"
exit 1
}
testing() {
echo " + $*"
}
hint() {
echo " ? CHECK: $*"
}
fail() {
echo " - ERROR: $*"
exit 1
}
BASEDIR="$(dirname "$(readlink -f "$0")")"
REGEX_SHEBANG="^#!\s*(.*)$"
excluded() {
FILE=$1
if [ "$FILE" = "$BASEDIR/db/dbenv" ]; then
echo "1"
else
echo "0"
fi
}
# Find all the script files both executable and ones with shebang
FILES=$({ \
find "$BASEDIR" -not -path '*/.*' -type f -executable -print || die "Search: find"
grep -rIzl --exclude-dir='.*' '^#![:blank:]' "$BASEDIR" || die "Search: Grep"
} | sort | uniq)
echo "Testing:"
for FILE in $FILES; do
echo "* $FILE"
testing "Executable"
if [ ! -x "$FILE" ]; then
fail "Not Executable"
fi
testing "Shebang"
FIRST_LINE=$(head -1 "$FILE")
if [[ $FIRST_LINE =~ $REGEX_SHEBANG ]]; then
SHEBANG_EXEC="${BASH_REMATCH[1]}"
SHELL=
if [ "$SHEBANG_EXEC" = "/bin/bash" ]; then
SHELL="bash"
bash -n -u "$FILE" || fail "$SHELL: Invalid Script"
sh -n "$FILE" > /dev/null 2>&1 && hint "Could be run in bash?"
elif [ "$SHEBANG_EXEC" = "/usr/bin/env bash" ]; then
SHELL="bash"
bash -n -u "$FILE" || fail "$SHELL: Invalid Script"
sh -n "$FILE" > /dev/null 2>&1 && hint "Could be run in bash?"
elif [ "$SHEBANG_EXEC" = "/bin/sh" ]; then
SHELL="sh"
sh -n "$FILE" || fail "$SHELL: Invalid Script"
elif [ "$SHEBANG_EXEC" = "/usr/bin/env sh" ]; then
SHELL="sh"
sh -n "$FILE" || fail "$SHELL: Invalid Script"
else
fail "Unknown Shell: $SHEBANG_EXEC"
fi
else
fail "No Shebang: $FIRST_LINE"
fi
## Run Shellcheck on the file
if [ "$(excluded "$FILE")" = "1" ]; then
hint "Excluded from shellcheck"
else
shellcheck "$FILE" || fail "Shellcheck"
fi
done