How can I do this better?

Hello,

Still learning over here. I made the below based on what I've learned so far and what I've stolen from others. It updates my dynDNS addresses, both IPv4 and v6. I'm looking to see what I could have done better or how those more experienced than me would have done it. Thanks for your time!
(It wont let me post links cuz I'm still new here so I dropped one of the 't's in the URLs on purpose)

#! /bin/bash

IP6temp=$(ip -o -6 addr show eno1 | sed -e 's/^.*inet6 \([^ ]\+\).*/\1/' | grep 64 | grep 200) #All of this is to get one globally unique IPv6
#echo $IP6temp #debugging
IP6=$(echo $IP6temp | cut -f1 -d'/') #get rid of the /notation
#echo "IPv6 Address is $IP6"
last_ip_file="/tmp/last_ip"
last_ip=`cat $last_ip_file`
ip=$(curl -s htp://dynamicdns.park-your-domain.com/getip)
#echo "IPV4 is $ip"
if [ "$ip" == "$last_ip" ]; then
        #echo "IP Still same, not need to update."
        exit 0
fi

response=$(curl -s "<HTPS://<UserName>:<API-Key>@members.dyndns.org/v3/update?hostname=<domain>.better-than.tv&myip=$ip,$IP6")
#echo $response
response=$(curl -s "htps://<UserName>:<API-Key>@members.dyndns.org/v3/update?hostname=<other domain>.better-than.tv&myip=$ip")
echo $ip > $last_ip_file
IP6=$(echo $IP6temp | cut -f1 -d'/') #get rid of the /notation

can be replaced by the much more efficient variable expansion:

IP6=${IP6temp%%/*} #get rid of the /notation
1 Like

I can't speak for the curl s but the following gets rid of unecessary grep s, sed and cat .

#! /bin/bash

IP6=$(ip -6 -o addr show eno1 | awk '$4 ~ /64$/ && $4 ~ /200/ {$0 = $4;} sub("/.*","")') #All of this is to get one globally unique IPv6
#echo "IPv6 Address is $IP6"
last_ip_file="/tmp/last_ip"
last_ip=$( <$last_ip_file )
ip=$(curl -s htp://dynamicdns.park-your-domain.com/getip)
#echo "IPV4 is $ip"
if [ "$ip" == "$last_ip" ]; then
        #echo "IP Still same, not need to update."
        exit 0
fi

response=$(curl -s "<HTPS://<UserName>:<API-Key>@members.dyndns.org/v3/update?hostname=<domain>.better-than.tv&myip=$ip,$IP6")
#echo $response
response=$(curl -s "htps://<UserName>:<API-Key>@members.dyndns.org/v3/update?hostname=<other domain>.better-than.tv&myip=$ip")
echo $ip > $last_ip_file

The only problem I have with my solution is what if the output format of ip changes so that your address is no longer in field 4?

Andrew

last_ip=`cat $last_ip_file`

is certainly more expensive than

last_ip=$( <$last_ip_file )

But in the case it is one line, the following works, too:

read last_ip <$last_ip_file

It would even allow the following two-lines expansion:

{
read last_ip
read last_ip6
} <$last_ip_file

And writing the two-lines file would be like

{
echo "$ip"
echo "$IP6"
} >$last_ip_file

or

echo "\
$ip
$IP6" > $last_ip_file

or

printf "%s\n" "$ip" "$IP6" > $last_ip_file