bash 字符串比较

发布于 2025-01-03 23:57:20 字数 615 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠北森屋 2025-01-10 23:57:20
if [ "$IP1"<>"$REMOTE" ]

不等于运算符是!=,两边都需要空格。它们不仅仅是为了美观,而且是必需的。

if [ "$IP1" != "$REMOTE" ]
if [ "$IP1"<>"$REMOTE" ]

The not equal operator is !=, and you need spaces on both sides. They're not just for looks, they're required.

if [ "$IP1" != "$REMOTE" ]
菩提树下叶撕阳。 2025-01-10 23:57:20
if [ "$IP1"<>"$REMOTE" ]

将其更改为:

if [ "$IP1" != "$REMOTE" ]
if [ "$IP1"<>"$REMOTE" ]

Change this to:

if [ "$IP1" != "$REMOTE" ]
行至春深 2025-01-10 23:57:20

我认为 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文