Comparing two IP Addresses in Bash

前端 未结 2 1299
小鲜肉
小鲜肉 2021-01-19 18:36

I am trying to find if two ip addresses are the same or not. I admit that I am new with bash, but I see no reason this would not work:

if [[ \"$IPAddress\" !         


        
2条回答
  •  生来不讨喜
    2021-01-19 18:45

    While your command should work, you can use the simple test operator (just a single bracket). The advantage is that it will work with any (POSIX) shell. However, the [[ operator should work too.

    Can you reproduce this little example? (Should output 'yes'):

    IPAddress="127.0.0.1"
    OLDIPAddress="127.0.0.1"
    
    if [ "$IPAddress" != "$OLDIPAddress" ] ; then 
        echo "no"
    else
        echo "yes"
    fi
    

提交回复
热议问题