Bash string comparison not working

Bash string comparison not working

Post by Julie Ventimigli » Thu, 23 Jan 2003 06:54:33



I want to grab a copy of the website and store the md5 signature of the
index page in a file I am calling "journal.txt". Every few hours, I want to
fire the script and grab a fresh copy of the page. If the signature of the
page does not match the last entry in the journal, I will tell it to send me
an email. The problem is that I can't get it to recognize that the signature
is different. Echoing both variables to the screen shows that the correct
values are being stored in each variable, so I know it's playing with the
right strings. Does anybody know why $OLDHASH and $NEWHASH are being treated
as the same even when they are different? Is it possible that text strings
need some kind of special treatment?

Here's the part that's giving me problems:

----------------begin--------------------
!# /bin/bash

wget http://www.website.com

OLDHASH=`tail -1 journal.txt`
echo "The old signature is $OLDHASH"

NEWHASH=`md5sum index.html`
echo "The new signature is $NEWHASH"

md5sum index.html >>journal.txt
mv index.html index.html-$(date +%y%m%d%H%M)

  if [ "$OLDHASH"=="$NEWHASH" ] ; then
       echo "No changes detected"
  fi

------------------end------------------------

Thank You for your time
--
Julie Ventimiglia

 
 
 

Bash string comparison not working

Post by Barry Margoli » Thu, 23 Jan 2003 07:19:31




>I want to grab a copy of the website and store the md5 signature of the
>index page in a file I am calling "journal.txt". Every few hours, I want to
>fire the script and grab a fresh copy of the page. If the signature of the
>page does not match the last entry in the journal, I will tell it to send me
>an email. The problem is that I can't get it to recognize that the signature
>is different. Echoing both variables to the screen shows that the correct
>values are being stored in each variable, so I know it's playing with the
>right strings. Does anybody know why $OLDHASH and $NEWHASH are being treated
>as the same even when they are different? Is it possible that text strings
>need some kind of special treatment?

No, the problem is with your test syntax.  It should be:

if [ "$OLDHASH" = "$NEWHASH" ] ; then

Quote:>  if [ "$OLDHASH"=="$NEWHASH" ] ; then

Since you left out the whitespace, it was just a single string argument;
the test is true if that argument isn't an empty string.  Also, even with
the whitespace added, you had == instead of =.

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Bash string comparison not working

Post by Julie Ventimigli » Thu, 23 Jan 2003 08:59:22





> >I want to grab a copy of the website and store the md5 signature of the
> >index page in a file I am calling "journal.txt". Every few hours, I want
to
> >fire the script and grab a fresh copy of the page. If the signature of
the
> >page does not match the last entry in the journal, I will tell it to send
me
> >an email. The problem is that I can't get it to recognize that the
signature
> >is different. Echoing both variables to the screen shows that the correct
> >values are being stored in each variable, so I know it's playing with the
> >right strings. Does anybody know why $OLDHASH and $NEWHASH are being
treated
> >as the same even when they are different? Is it possible that text
strings
> >need some kind of special treatment?

> No, the problem is with your test syntax.  It should be:

> if [ "$OLDHASH" = "$NEWHASH" ] ; then

> >  if [ "$OLDHASH"=="$NEWHASH" ] ; then

> Since you left out the whitespace, it was just a single string argument;
> the test is true if that argument isn't an empty string.  Also, even with
> the whitespace added, you had == instead of =.

> --

> Genuity, Woburn, MA
> *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to
newsgroups.
> Please DON'T copy followups to me -- I'll assume it wasn't posted to the

group.

Thank you, that fixed the problem!

--
Julie Ventimiglia

 
 
 

Bash string comparison not working

Post by Dan Merc » Fri, 24 Jan 2003 02:12:29






>>I want to grab a copy of the website and store the md5 signature of the
>>index page in a file I am calling "journal.txt". Every few hours, I want to
>>fire the script and grab a fresh copy of the page. If the signature of the
>>page does not match the last entry in the journal, I will tell it to send me
>>an email. The problem is that I can't get it to recognize that the signature
>>is different. Echoing both variables to the screen shows that the correct
>>values are being stored in each variable, so I know it's playing with the
>>right strings. Does anybody know why $OLDHASH and $NEWHASH are being treated
>>as the same even when they are different? Is it possible that text strings
>>need some kind of special treatment?

> No, the problem is with your test syntax.  It should be:

> if [ "$OLDHASH" = "$NEWHASH" ] ; then

>>  if [ "$OLDHASH"=="$NEWHASH" ] ; then

> Since you left out the whitespace, it was just a single string argument;
> the test is true if that argument isn't an empty string.  Also, even with
> the whitespace added, you had == instead of =.

== works for bash2.  It's unclear which he is using.

--
Dan Mercer

If responding by email, include the phrase 'from usenet'
in the subject line to avoid spam filtering.

> --

> Genuity, Woburn, MA
> *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
> Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

Opinions expressed here are my own and may not represent those of my employer.
 
 
 

1. string comparison not working

Hello,

I'm having problems with string comparison. One of the strings is a
command argument passed to the script as $1. The other string is
a word extracted from a file:

var=`awk '/WORD/{print $3}' file`

if [ "$1" = "$var" ]; then
   echo "strings are identical"
else
   echo "strings are NOT identical"
fi

Here is what happens when I run the sript in debugging mode: set -x

++ awk '/WORD/{print $3}' file
+ var=argument
 ']'' argument = argument
+ echo strings are NOT identical
strings are NOT identical
+ set -

Even though both strings are the same I always get the "strings are NOT
identical". I use bash 2 under Linux. Thank you.

Dave

2. IBM RS/6000 7006 (42T) and GXT500 Grafikadapter

3. BASH BASH BASH BASH BASH BASH BASH BASH BASH BASH

4. slip connection not working

5. string comparison problem in bash 3.00

6. UREP

7. x preceeding strings in bash comparisons

8. 5.24 bogomips w/Cyrix. Ouch

9. String comparison in bash

10. Riddle me this - when is a ksh string not a string?

11. string comparison problem

12. String Comparison

13. Multiple String Comparison in Ksh.