比较Bash中的两个IP地址 [英] Comparing two IP Addresses in Bash
问题描述
我正在尝试查找两个IP地址是否相同.我承认我是bash的新手,但我认为没有任何理由不起作用:
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" != "$OLDIPAddress" ]]
then
echo "IP Not the Same"
else
echo "IP Same"
fi
出于测试目的,我什至对两个变量的值进行了硬编码,但它们仍然不一样.我知道您并不总是能看到自己的错别字,但我不明白为什么这是行不通的.有什么想法吗?
For testing purposes, I have even hard coded the values for the two variables, and they still don't show up as the same. I know you don't always see your own typos, but I do not see why this would not work. Any ideas?
推荐答案
虽然命令应该起作用,但是您可以使用简单的测试运算符(只需放在一个括号中).优点是它可以与任何(POSIX)Shell一起使用.但是,[[
运算符也应该起作用.
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
这篇关于比较Bash中的两个IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!