Last active
July 8, 2018 02:53
-
-
Save tjcelaya/c14cfb8af382f225936c115b8c2206d5 to your computer and use it in GitHub Desktop.
fdns.sh - upserts blocks of hosts into /etc/hosts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
set -x | |
FDNS_HOSTS_FILE=${1:-.fdns} | |
FDNS_HOSTS_KEY=$(echo $FDNS_HOSTS_FILE | sed 's/[^a-zA-Z0-9]//g') | |
echo updating $FDNS_HOSTS_KEY block | |
FDNS_MARKER_START='^###---FDNS-START-'"${FDNS_HOSTS_FILE}"'---###$' | |
FDNS_MARKER_END='^###---FDNS-END-'"${FDNS_HOSTS_FILE}"'---###$' | |
if [[ -z ${FDNS_HOSTS_FILE} || ! -f ${FDNS_HOSTS_FILE} ]]; then | |
echo "hosts file not provided or missing: got $FDNS_HOSTS_FILE" | |
exit 1 | |
fi | |
if grep -q ${FDNS_HOSTS_FILE} /etc/hosts; then | |
sudo sed -i -e "/${FDNS_MARKER_START}/,/${FDNS_MARKER_END}/{ /$FDNS_MARKER_START/{p; r ${FDNS_HOSTS_FILE} | |
}; /$FDNS_MARKER_END/p; d }" /etc/hosts | |
else | |
sudo bash -c "echo '"${FDNS_MARKER_START:1:${#FDNS_MARKER_START}-2}"' >> /etc/hosts" | |
sudo bash -c "cat "${FDNS_HOSTS_FILE}" >> /etc/hosts" | |
sudo bash -c "echo '"${FDNS_MARKER_END:1:${#FDNS_MARKER_END}-2}"' >> /etc/hosts" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment