简体   繁体   中英

Bash script to compare ip addresses from web and local file

I'm fairly new to bash and am still trying to figure some stuff out. Currently I'm writing a simple script to grab the ip address value from a website (via wget) and compare it with the address I already have stored on the machine to check for address changes. This is what I have so far:

#!/bin/bash
currentIp=$(cat /root/ip.log)
if "$currentIp" == $(wget -O - -q -nv --delete-after www.icanhazip.com)
then
    echo IP address is unchanged
else
    echo IP address has changed
fi

But, after running it, bash is replacing the variable currentIp with its value and then trying to run it as a command (I have replaced my ip address with x's in this example):

line 3: x.x.x.x: command not found
IP address has changed

You have a little syntax error in your expression. Use this instead:

if [ "$currentIp" == $(wget -O - -q -nv --delete-after www.icanhazip.com) ] ; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM