C0ppert0p
C0ppert0p

Reputation: 702

bash string comparisons

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

Upvotes: 5

Views: 7420

Answers (3)

cEz
cEz

Reputation: 5062

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

Change this to:

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

Upvotes: 3

Diego
Diego

Reputation: 1569

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?

Upvotes: 2

John Kugelman
John Kugelman

Reputation: 361605

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" ]

Upvotes: 8

Related Questions