Only update hostname when necessary

* If $IP was empty then HOSTNAME would remain set to the current HOSTNAME
  * This allowed the redundant set-hostname call
This commit is contained in:
2Shirt 2019-04-11 21:14:32 -07:00
parent d113d710a7
commit c3ebdee5d0
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -1,25 +1,23 @@
## .update_network ##
#!/bin/env bash #!/bin/env bash
# #
## Connect to network and update hostname ## Setup network and update hostname
# Add saved networks to NetworkManager # Add saved networks to NetworkManager
sudo setup-wifi sudo setup-wifi
sudo systemctl restart NetworkManager sudo systemctl restart NetworkManager
# Set hostname
IP="$(ip a show scope global \ IP="$(ip a show scope global \
| grep inet \ | grep inet \
| head -1 \ | head -1 \
| sed -r 's#.*inet ([0-9]+.[0-9]+.[0-9]+.[0-9]+.)/.*#\1#')" | sed -r 's#.*inet ([0-9]+.[0-9]+.[0-9]+.[0-9]+.)/.*#\1#')"
if [[ "${IP:+x}" ]]; then if [[ "${IP:+x}" ]]; then
HOSTNAME="$(dig +noall +answer +short -x "$IP" \ NEW_HOSTNAME="$(dig +noall +answer +short -x "$IP" \
| grep -v ';' \ | grep -v ';' \
| head -1 \ | head -1 \
| sed 's/\.$//')" | sed 's/\.$//')"
fi fi
if [[ "${NEW_HOSTNAME:+x}" ]]; then
# Set hostname sudo hostnamectl set-hostname "${NEW_HOSTNAME}"
if [[ "${HOSTNAME:+x}" ]]; then
sudo hostnamectl set-hostname "${HOSTNAME}"
fi fi