Instantly share code, notes, and snippets.

Embed
What would you like to do?
Appends a /etc/hosts style file onto your existing /etc/hosts file after backing it up
#!/bin/bash
# USAGE
# $> sudo ./sync-hosts-file
# Run this command in a directory where you have a hosts.append file
# which has hosts in the /etc/hosts style.
if [ "$(id -u)" != "0" ]; then
echo "You need to be root to execute this script"
exit 1
fi
HOSTS_BACKUP="/etc/hosts.sync.bak"
HOSTS_FILE="/etc/hosts"
ADDITIONAL_HOSTS="hosts.append"
if [ ! -f $HOSTS_BACKUP ];
then
echo "This is the first sync attempt. Copying over $HOSTS_FILE to $HOSTS_BACKUP";
cp $HOSTS_FILE $HOSTS_BACKUP
fi
cat $HOSTS_BACKUP > $HOSTS_FILE
cat $ADDITIONAL_HOSTS >> $HOSTS_FILE
echo "Done. Added original and typeset hosts to $HOSTS_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment