I have the following script that is trying to match and IP address with a value in a file
#!/usr/bin/env bash
#To find the IP via ping
ping=$(ping federicolivieri.noip.me -c 1)
#To cut the result
ip=$(echo ${ping} | awk '{print $3}' | rev | cut -c2- | rev | cut -c2-)
#dnsdist variable
dnsdist_ip=$(awk 'END{print $1}' /etc/dnsdist/dnsdist.conf | cut -c 19- | rev | cut -c 2- | rev)
if [ "$ip" -eq "$dnsdist_ip" ]; then
echo "ciao"
else
echo "newServer{address="`echo ${ip}`", name="raspi"}" >> /etc/dnsdist/dnsdist.conf
fi
However, when I run the script I get this error
root@raspberrypi:/etc/myscripts# ./noip.sh
./noip.sh: line 10: [: 2.31.237.195: integer expression expected
I understood that the script expect a integer numeric value but as you know, IP address as "dots"
How can I workaround this problem?
man testcut -c19-will break. You'd be safer matching against the field nameaddresslike this -dnsdist_ip=$(sed -n '$s!^.*address=\([1-9][0-9.]*\).*!\1!p' /etc/dnsdist/dnsdist.conf)cutusage here is brittle. You're already usingawkto grab only the first whitespace-separated field; why do you need to drop the last character of it? (If you really do need to drop the last character, I'd still recommend ending that pipeline with theawkcommand and following up withdnsdist_ip=${dnsdist_ip%?}instead of the non-portablerev.)