Shell script to easily update your Everdo API address after switching to a different network

One minor annoyance I have is having to update my ip address on both my laptop and my phone after changing to another wi-fi network in order to sync properly. Made this script that sets the ip in the config file, then shows a persistent notification (using dunst) so I can update it on my phone. In Hyprland, I’ve set it to run during boot & launch Everdo if it succeeds, so that I only have to change it on my phone. Haven’t written shell scripts in a long time so used AI to write this. Usingjq would’ve been cleaner but didn’t want it creating temporary files just to update one value.

#!/bin/bash

# Get current active local IP
IP=$(ip -4 addr show | awk '/state UP/{iface=$2} /inet /{if (iface) {sub(/\/.*/,"",$2); print $2; exit}}')
CONFIG_FILE="$HOME/.config/everdo/config.json"

if [ -z "$IP" ]; then
    dunstify -t 0 "Everdo Config Update" "Failed to detect local IP address. Connect to a network, run evrd-addr, then launch Everdo"
    exit 1
fi

# Get current host value from config
CURRENT_HOST=$(grep -oP '"host": *"\K[0-9.]+' "$CONFIG_FILE")

# Compare and update only if different
if [ "$IP" != "$CURRENT_HOST" ]; then
    sed -i "s/\"host\": *\"[0-9.]\+\"/\"host\": \"$IP\"/" "$CONFIG_FILE"
    dunstify -t 0 "Everdo Config Updated" "Host changed to $IP. Change this on your other devices too."
fi