forked from stay-sharp/hosts_for_google_service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkhosts.sh
executable file
·103 lines (88 loc) · 2.35 KB
/
checkhosts.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
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash
# https://github.com/racaljk/hosts
LINE_BREAK=0
FORMAT_BREAK=0
DATE_BREAK=0
chk_eol()
{
echo -e " * Check line endings:\n"
if file "$1" | grep -q "CRLF"; then
echo -e "\033[41mDOS line endings have appeared, " \
"it must be coverted now!\033[0m\n\n"
LINE_BREAK=1
else
echo -e "\033[42mAll is well!\033[0m\n\n"
fi
}
# Check TAB on hosts records.
# Check leading and trailing whitespace.
#
chk_format()
{
echo -e " * Check hosts format:\n"
# Filter all hosts records.
cat "$1" | grep -Pv "^\s*#" | grep -P "(\d+\.){3}\d+" > 1.swp
# Trailing whitespace detection.
grep -P "[ \t]+$" "$1" >> 1.swp
# Filter good format hosts records.
cat "$1" | grep -Pv "^\s*#" | grep -P "^(\d+\.){3}\d+\t\w" > 2.swp
if ! diff 1.swp 2.swp > 0.swp; then
echo -e "\033[41mhosts format mismatch! " \
"The following rules should be normalized:\033[0m"
cat 0.swp
FORMAT_BREAK=1
else
echo -e "\033[42mAll is well!\033[0m"
fi
echo -e "\n"
rm -f 0.swp 1.swp 2.swp
}
chk_date()
{
# system date
local real_date=$(date +%F)
# The last change of the hosts file.
local repo_date=$(git log --date=short "$1" | grep -Pom1 "\d+-\d+-\d+")
# date string in hosts file.
local hosts_date=$(grep -Po "\d+-\d+-\d+" "$1")
echo -e " * Check hosts date:\n"
# check if hosts file changes
if git diff --exit-code "$1" &> /dev/null; then
# hosts file is not changed, compare file's date with git log.
if [ "$repo_date" != "$hosts_date" ]; then
echo -e "\033[41mhosts date mismatch, last modified " \
"is $repo_date, but hosts tells " \
"$hosts_date\033[0m\n\n"
DATE_BREAK=1
else
echo -e "\033[42mAll is well!\033[0m\n\n"
fi
else
# hosts file is being editing, and has not been committed.
# Compare file's date with the system date.
if [ "$real_date" != "$hosts_date" ]; then
echo -e "\033[41mhosts date mismatch, last modified " \
"is $real_date, but hosts tells " \
"$hosts_date\033[0m\n\n"
DATE_BREAK=1
else
echo -e "\033[42mAll is well!\033[0m\n\n"
fi
fi
}
result()
{
echo -e "Result (1 = yes, 0 = no):\n"
echo "line endings break? [ $LINE_BREAK ]"
echo "hosts format mismatch? [ $FORMAT_BREAK ]"
echo "hosts date mismatch? [ $DATE_BREAK ]"
exit $(( $LINE_BREAK + $FORMAT_BREAK + $DATE_BREAK ))
}
if [ -z "$1" ]; then
echo "Usage: $0 [hosts-file]"
exit 4
fi
chk_eol "$1"
chk_format "$1"
chk_date "$1"
result