bash 字符串比较
我在 bash 中遇到字符串比较问题: 下面的示例告诉我被比较的两个字符串是不同的。他们不是。
REMOTE=`grep remote /etc/hosts|cut -f1| tr -d ' '`
IP1=`/opt/local/bin/lynx -accept_all_cookies -dump
http://whatismyip.com | grep "Your IP Address Is"| cut -d" " -f8 |
tr -d ' '`
if [ "$IP1"<>"$REMOTE" ]
then
echo "IP1 -ne REMOTE"
echo "=>"$IP1"<="
echo "=>"$REMOTE"<="
sudo cp /etc/hosts /etc/hosts.bkp
sudo gsed -i 's/$IP/$REMOTE/g' /etc/hosts
fi
IP1 68.49.172.18
REMOTE 68.49.172.18
IP1 -ne REMOTE
=>68.49.172.18<=
=>69.49.172.18<=
I'm having an issue with string comparisons in bash:
the following sample is telling me the two strings being compared are different. they are not.
REMOTE=`grep remote /etc/hosts|cut -f1| tr -d ' '`
IP1=`/opt/local/bin/lynx -accept_all_cookies -dump
http://whatismyip.com | grep "Your IP Address Is"| cut -d" " -f8 |
tr -d ' '`
if [ "$IP1"<>"$REMOTE" ]
then
echo "IP1 -ne REMOTE"
echo "=>"$IP1"<="
echo "=>"$REMOTE"<="
sudo cp /etc/hosts /etc/hosts.bkp
sudo gsed -i 's/$IP/$REMOTE/g' /etc/hosts
fi
IP1 68.49.172.18
REMOTE 68.49.172.18
IP1 -ne REMOTE
=>68.49.172.18<=
=>69.49.172.18<=
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不等于运算符是
!=
,两边都需要空格。它们不仅仅是为了美观,而且是必需的。The not equal operator is
!=
, and you need spaces on both sides. They're not just for looks, they're required.将其更改为:
Change this to:
我认为 bash 中的字符串比较运算符是 != 而不是 <>。你能做个测试看看这是否是你遇到的问题吗?
I think the string comparison operator in bash is != and not <>. Can you make a test to see if that is the problem you are having?