3
3
WORKDIR=" /opt/valet-linux"
4
4
DNSFILE=" ${WORKDIR} /dns-servers"
5
5
LOGFILE=" ${WORKDIR} /watch.log"
6
- PIDS=" "
6
+ RESOLV=" /etc/resolv.conf"
7
+
8
+ function unique {
9
+ # Function to remove duplicated lines (even when they are not contiguous)
10
+ # cat -n puts line numbers
11
+ # sort -uk2 sort and remove duplicates (ignoring line numbers)
12
+ # sort -nk1 sort by line number
13
+ # cut -f2- remove line numbers
14
+ cat -n | sort -uk2 | sort -nk1 | cut -f2-
15
+ }
7
16
8
17
function updateNameservers() {
9
18
# Read all of the nameserver files at once, filter lines starting with 'nameserver'
10
19
# and exclude the ones containing 127.0.0.1 (localhost)
11
20
12
- cat " ${FILES[@]} " | grep -i ' ^nameserver' | grep -v ' 127.0.0.1' | tee " $DNSFILE " & > /dev/null
21
+ cat " ${FILES[@]} " | grep -i ' ^nameserver' | grep -v ' 127.0.0.' | unique | tee " $DNSFILE " & > /dev/null
22
+
23
+ # Add "search" and "domain" directives to /etc/resolv.conf
24
+ chattr -i " $RESOLV " && \
25
+ cat " ${FILES[@]} " | grep -e ' ^search' -e ' ^domain' | unique | tee " $RESOLV " & > /dev/null && \
26
+ echo ' 127.0.0.1' >> " $RESOLV " && \
27
+ chattr +i " $RESOLV "
13
28
}
14
29
15
30
function watchFiles() {
@@ -37,7 +52,6 @@ function main() {
37
52
38
53
for FILE in " ${FILES[@]} " ; do
39
54
watchFiles " $FILE " &
40
- PIDS=" $PIDS $! "
41
55
done
42
56
43
57
# Find nameserver files in the /run/resolvconf/interface folder (as they do not have a standard name)
@@ -56,4 +70,4 @@ if [[ ! $(pgrep -f 'inotifywait -q -m -e modify') ]]; then
56
70
main
57
71
fi
58
72
59
- exit 0
73
+ exit 0
0 commit comments