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