Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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<=
like image 980
C0ppert0p Avatar asked Feb 09 '12 22:02

C0ppert0p


3 Answers

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" ]
like image 184
John Kugelman Avatar answered Sep 18 '22 02:09

John Kugelman


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

Change this to:

if [ "$IP1" != "$REMOTE" ]
like image 31
cEz Avatar answered Sep 18 '22 02:09

cEz


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?

like image 28
Diego Avatar answered Sep 21 '22 02:09

Diego