gpt4 book ai didi

string - bash 字符串比较

转载 作者:行者123 更新时间:2023-11-29 09:02:11 25 4
gpt4 key购买 nike

我在 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<=

最佳答案

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

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

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

关于string - bash 字符串比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9220177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com