-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck
More file actions
executable file
·108 lines (86 loc) · 2.46 KB
/
Copy pathcheck
File metadata and controls
executable file
·108 lines (86 loc) · 2.46 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#! /bin/bash
die() {
if [ -t 2 ]; then
printf '\n\033[31m✗\033[0m %s\n' "$*" >&2
else
printf '\n✗ %s\n' "$*" >&2
fi
exit 1
}
success() {
if [ -t 1 ]; then
printf '\n\033[32m✓\033[0m %s\n' "$*"
else
printf '\n✓ %s\n' "$*"
fi
}
info() {
if [ -t 1 ]; then
printf '\n\033[32m→\033[0m %s\n' "$*"
else
printf '\n→ %s\n' "$*"
fi
}
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
mapfile -t FILES < <({ \
find "$BASEDIR" -not -path '*/.*' -type f -executable -print || die "Search: find"
while IFS= read -r SHEBANG_FILE; do
IFS= read -r FIRST_LINE < "$SHEBANG_FILE" || continue
case $FIRST_LINE in
'#!'*)
printf '%s\n' "$SHEBANG_FILE"
;;
esac
done < <(find "$BASEDIR" -not -path '*/.*' -type f -print) || die "Search: Shebang"
} | sort | uniq)
[ "${#FILES[@]}" -gt 0 ] || die "No script files found in ${BASEDIR}"
info "Testing:"
for FILE in "${FILES[@]}"; do
info "* $FILE"
info "Executable"
if [ ! -x "$FILE" ]; then
die "Not Executable: ${FILE}"
fi
info "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" || die "$SHELL: Invalid Script: ${FILE}"
sh -n "$FILE" > /dev/null 2>&1 && info "Hint: Could be run in sh?"
elif [ "$SHEBANG_EXEC" = "/usr/bin/env bash" ]; then
SHELL="bash"
bash -n -u "$FILE" || die "$SHELL: Invalid Script: ${FILE}"
sh -n "$FILE" > /dev/null 2>&1 && info "Hint: Could be run in sh?"
elif [ "$SHEBANG_EXEC" = "/bin/sh" ]; then
SHELL="sh"
sh -n "$FILE" || die "$SHELL: Invalid Script: ${FILE}"
elif [ "$SHEBANG_EXEC" = "/usr/bin/env sh" ]; then
SHELL="sh"
sh -n "$FILE" || die "$SHELL: Invalid Script: ${FILE}"
else
die "Unknown Shell: $SHEBANG_EXEC in ${FILE}"
fi
else
die "No Shebang: $FIRST_LINE in ${FILE}"
fi
## Run Shellcheck on the file
if [ "$(excluded "$FILE")" = "1" ]; then
info "Excluded from shellcheck"
else
shellcheck "$FILE" || die "Shellcheck: ${FILE}"
fi
done
success "All checks passed"