2012-02-09 3 views
5

Sto riscontrando un problema con i confronti tra stringhe in bash: il seguente esempio indica che le due stringhe confrontate sono diverse. non sono.confronti stringa 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<= 

risposta

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

Il pari operatore non è !=, e avete bisogno di spazi su entrambi i lati. Non sono solo per aspetto, sono obbligatori.

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

Penso che l'operatore di confronto delle stringhe in bash sia! = E non <>. Puoi fare un test per vedere se questo è il problema che stai avendo?

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

Modifica questo:

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