-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathaci-trailing-newline
executable file
·73 lines (57 loc) · 1.29 KB
/
aci-trailing-newline
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
#!/bin/sh -u
NAME="aci-trailing-newline"
print_usage() {
echo "Usage: ${NAME} -n file"
echo " ${NAME} -1 file"
echo " ${NAME} -v"
echo
echo "Validates if a file has at least 1 or exactly only 1 trailing newline."
echo "Returns 0, if condition is met, otherwise > 0"
echo
echo " -n Must have at least 1 trailing newline at the end of the file."
echo " -1 Must have exactly only 1 trailing newline at the end of the file."
echo " -v Show version"
exit 1
}
print_version() {
echo "${NAME} v0.1 (2016-08-21)"
echo "Written by cytopia <[email protected]>"
echo "https://github.com/cytopia"
}
if [ "${#}" = "1" ] && [ "${1}" = "-v" ]; then
print_version
exit 0
fi
if [ "${#}" != "2" ]; then
print_usage
exit 1
fi
if [ "${1}" != "-n" ] && [ "${1}" != "-1" ]; then
print_usage
exit 1
fi
OPTION="${1}"
FILE="${2}"
#
# Validate file
#
if [ ! -f "${FILE}" ]; then
echo "No such file: ${FILE}"
exit 1
fi
if [ ! -r "${FILE}" ]; then
echo "Cannot read file: ${FILE}"
exit 1
fi
# No trailing newline at the end of the file
if ! tail -c 1 "${FILE}" | grep -qE "^$"; then
exit 2
fi
# Check if only 1 trailing newline
if [ "${OPTION}" = "-1" ]; then
# At least two newlines at the end of the file
if tail -c 2 "${FILE}" | grep -qE "^$"; then
exit 3
fi
fi
exit 0