LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-13-2005, 12:20 PM   #1
The_JinJ
Member
 
Registered: Apr 2004
Location: Scotland
Distribution: Suse, OpenWRT
Posts: 299

Rep: Reputation: 30
Bash - weird results from if


I am writing a wee script to get an ip and compare to an existing file - the results of the 2 cat commands are identical but no matter what the files contain it will always treat this as false....If both files have exactly the same it will never print Hello, same if both are different.....heellpp!!

#!/bin/sh
#
# Get external IP address #
/usr/bin/wget http://www.xxxx/ip.php

result1=$(cat ip.php)
result2=$(cat ip.old)

echo $result1
echo $result2

if [ '$result1' = '$result2' ]; then
echo "hello"
fi

mv ip.php ip.old
 
Old 02-13-2005, 01:45 PM   #2
DoubleOTeC
Member
 
Registered: Aug 2003
Location: Dominica
Distribution: RedHat, FC1, FC3, FC4
Posts: 266

Rep: Reputation: 30
I think you're using the wrong comparison operator.

i think it should be if [ "result1" == "result2" ];

lemme know...
 
Old 02-13-2005, 01:56 PM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
The single equal operator is correct.

Use double quotes around your variables in the if condition.

Code:
#!/bin/sh

result1=`cat ip.php`
result2=`cat ip.old`

echo "Result1 is [$result1]"
echo "Result2 is [$result2]"

if [ "$result1" = "$result2" ]; then
  echo "Results are equal"
fi
Code:
$ ./testeq.sh
Result1 is [192.168.10.130]
Result2 is [192.168.10.130]
Results are equal
 
Old 02-13-2005, 02:11 PM   #4
The_JinJ
Member
 
Registered: Apr 2004
Location: Scotland
Distribution: Suse, OpenWRT
Posts: 299

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by crabboy
The single equal operator is correct.

Use double quotes around your variables in the if condition.

Code:
#!/bin/sh

result1=`cat ip.php`
result2=`cat ip.old`

echo "Result1 is [$result1]"
echo "Result2 is [$result2]"

if [ "$result1" = "$result2" ]; then
  echo "Results are equal"
fi
Code:
$ ./testeq.sh
Result1 is [192.168.10.130]
Result2 is [192.168.10.130]
Results are equal
Double quotes worked!! I swear it was giving an error earlier
Thanks for the assistance
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
eth0(WAN) and eth1(LAN) getting mixed with weird results Artanicus Linux - Networking 3 08-12-2005 07:23 AM
Weird Results for Xorg and DRI CEdstrom Slackware 3 10-17-2004 08:31 AM
SIL3112A Software-Raid weird hdparm -t results biatche Linux - Hardware 0 05-25-2004 05:33 AM
Weird results with netstat nekromancer Linux - Networking 3 02-17-2004 01:36 AM
Weird results from who and finger cauchy Linux - General 0 10-14-2001 10:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration