-
Notifications
You must be signed in to change notification settings - Fork 36
/
hosts-remove
executable file
·38 lines (33 loc) · 981 Bytes
/
hosts-remove
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
#!/bin/bash
set -eu
# Check Windows first as Windows can also have a /etc/hosts
if [ ! -f C:\\Windows\\System32\\drivers\\etc\\hosts ]; then
if [ ! -f /etc/hosts ]; then
echo "Failed to find hosts file."
exit
else
HOSTSFILE=/etc/hosts
fi
else
HOSTSFILE=C:\\Windows\\System32\\drivers\\etc\\hosts
fi
# When running on Windows as administrator, 'grep' and 'sed' may not be in the PATH
if [ ! -f /usr/bin/grep ]; then
GREPFILE=grep
else
GREPFILE=/usr/bin/grep
fi
if [ ! -f /usr/bin/sed ]; then
SEDFILE=sed
else
SEDFILE=/usr/bin/sed
fi
# TODO escalate / warn if file not accessible by current user
# Remove all of the lines we have added
$GREPFILE -v mediawiki-docker-dev $HOSTSFILE > ./.hosts.tmp
# Remove any excess whitespace
$SEDFILE -i.bak '/^[[:space:]]*$/{:a;$d;N;/\n[[:space:]]*$/ba}' ./.hosts.tmp
# Overwrite the hosts file with our new content
cat ./.hosts.tmp > $HOSTSFILE
# Delete the temp files
rm ./.hosts.tmp