Skip to content

Instantly share code, notes, and snippets.

@SmugZombie
Created April 8, 2020 17:47
Show Gist options
  • Save SmugZombie/b8960af54ed207bd08acb60f8f6bd945 to your computer and use it in GitHub Desktop.
Save SmugZombie/b8960af54ed207bd08acb60f8f6bd945 to your computer and use it in GitHub Desktop.
A simple script used to update WSL Ubuntu with host file entries you may not want Windows to have access to. place a .hosts file in the same working dir as the script it should be formatted like a hosts file as to not break your host resolution
#!/bin/bash
# Ron Egli / github.com/smugzombie
# A simple script used to update WSL Ubuntu with host file entries you may not want Windows to have access to
# place a .hosts file in the same working dir as the script
# - it should be formatted like a hosts file as to not break your host resolution
# Version 1.0
rootcheck () {
# A simple check to force sudo if not already root user. If unable quit.
if [ $(id -u) != "0" ]
then
sudo ./"$0" "$@"
exit $?
fi
}
# Doublecheck that the user is a sudo user, Otherwise Kill script
rootcheck
while IFS="" read -r hosts || [ -n "$hosts" ]
do
# Loop through the file, check to see if line already exists in hosts file, If not then we add it.
found=$(cat /etc/hosts | grep "$hosts")
if [[ -z "$found" ]]; then
# Add the line to the hosts file
echo "$hosts" >> /etc/hosts
# Let the user know the line has been added
echo "Added '$hosts' to /etc/hosts"
fi
done < .hosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment