forked from 0xmachos/bash-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshebang
executable file
·99 lines (67 loc) · 1.81 KB
/
shebang
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
#!/usr/bin/env bash
# bash-scripts/shebang
# shebang
# Checks if bash sctips contain a shebang on line one
# Check if file exists and is a bash scrip
# Checks for "#!" via grep
set -euo pipefail
# -e exit if any command returns non-zero status code
# -u prevent using undefined variables
# -o pipefail force pipelines to fail on first non-zero status code
### Define Colours ###
tput sgr0;
# reset colors
readonly RESET=$(tput sgr0)
readonly BOLD=$(tput bold)
readonly RED=$(tput setaf 1)
readonly GREEN=$(tput setaf 64)
readonly BLUE=$(tput setaf 4)
### END Colours ###
FATAL="${RED}FATAL${RESET}"
PASS="${GREEN}PASS${RESET}"
INFO="${BLUE}INFO${RESET}"
function usage {
echo -e "Usage: ./shebang.sh {file1} {file2} {file...}"
echo -e "[${INFO}] ./shebang.sh * checks all files in current directory "
}
function check_if_bash {
for file_to_check in "$@"; do
if [ ! -f "${file_to_check}" ]; then
# Check if file exists
echo -e "[${FATAL}] ${file_to_check} does not exist"
else
if file "${file_to_check}" | grep --quiet "shell" || file "${file_to_check}" | grep --quiet "bash"; then
bash_files+=("${file_to_check}")
else
echo -e "[${INFO}] ${file_to_check} is not a bash script"
fi
fi
done
}
function check_for_shebang {
for file in "${bash_files[@]}"; do
if head -1 "${file}" | grep --quiet "#\\!"; then
echo -e "[${PASS}] ${file} contains a shebang"
else
echo -e "[${FATAL}] ${file} does not have a shebang"
fi
done
echo -e "[${INFO}] ${#bash_files[@]} files checked"
}
function main {
if [[ $# -eq 0 ]] ; then
# If no args then print help
usage
exit 1
fi
bash_files=()
check_if_bash "$@"
if [ ${#bash_files[@]} -eq 0 ]; then
# If ERRORS empty then
echo -e "[${INFO}] No Bash files to check"
exit 0
else
check_for_shebang
fi
}
main "$@"