string comparison

string comparison

Post by Jorda » Wed, 08 Jan 2003 07:37:24



Hi and thank you in advance,

I'm new to Unix scripting and am struggling with what seems to be very
simple, could someone please help stop my madness.

set -x
for ftpserver in isg1uw aixprod
do
    case "$ftpserver" in
        aixprod)
            ...
            ;;
        isg1uw)
            ...
            ;;
    esac
    if "$ftpserver"=aixprod
    then
        print "print anything"
    fi
done

The case statement is evaluated and executed properly, but the if statement
always evaluates $ftpserver=aixprod to be true and continues to print "print
anything".  When I use set -x, I can see the following output

isg1uw=aixprod
bckupstat.sh[49]: isg1uw=aixprod:  not found.
+ aixprod=aixprod
bckupstat.sh[49]: aixprod=aixprod:  not found.

All help would be greatly appreciated

Thank you
Jordan

 
 
 

string comparison

Post by Jorda » Thu, 09 Jan 2003 00:09:37




> > Hi and thank you in advance,

> > I'm new to Unix scripting and am struggling with what seems to be very
> > simple, could someone please help stop my madness.

> > set -x
> > for ftpserver in isg1uw aixprod
> > do
> >    case "$ftpserver" in
> >        aixprod)
> >            ...
> >            ;;
> >        isg1uw)
> >            ...
> >            ;;
> >    esac
> >    if "$ftpserver"=aixprod
> >    then
> >        print "print anything"
> >    fi

>      if [[ $ftpserver = "aixprod" ]]
>      then
>           do_stuff
>      fi

> Regards,

> Nicholas

> --
> "Why shouldn't I top-post?"    http://www.aglami.com/tpfaq.html
> "Meanings are another story."  http://www.ifas.org/wa/glossolalia.html

I have tried your suggestion and still recieve
bckupstat3.sh[49]: [[aixprod3=aixprod3]]:  not found.

Any ideas?

 
 
 

string comparison

Post by Steve » Thu, 09 Jan 2003 00:07:50



> I'm new to Unix scripting and am struggling with what seems to be very
> simple, could someone please help stop my madness.
>     if "$ftpserver"=aixprod
>     then
>         print "print anything"
>     fi

Hi Jordan,

Nicholas already answered you on this, as it seems it is simply your
shell syntax that is causing a problem. Nick uses the double brackets
[[ like_this ]] around the test portion of your if statement. Many
people also right it as follows:

if test "${ftpserver}" = "aixprod"
then
   print "print anything"
fi

The "test" command is listed in the AIX Command Reference to show you
all the nice little flags you can use, and it tends to be a bit easier
to read than using brackets all over the place. Of course, everyone
has their own preferences and there are probably 4-5 ways to
accomplish the same thing.

For example, imagine the following if statement:

   if [[ "`oslevel | cut -f1 -d'.'`" = "4" ]]

Then rewrite it this way and it still works the same:

   if test "$(oslevel | cut -f1 -d'.')" = "4"

regards,

Steve N.

 
 
 

string comparison

Post by Jorda » Thu, 09 Jan 2003 00:27:21




Quote:> > I'm new to Unix scripting and am struggling with what seems to be very
> > simple, could someone please help stop my madness.
> >     if "$ftpserver"=aixprod
> >     then
> >         print "print anything"
> >     fi

> Hi Jordan,

> Nicholas already answered you on this, as it seems it is simply your
> shell syntax that is causing a problem. Nick uses the double brackets
> [[ like_this ]] around the test portion of your if statement. Many
> people also right it as follows:

> if test "${ftpserver}" = "aixprod"
> then
>    print "print anything"
> fi

> The "test" command is listed in the AIX Command Reference to show you
> all the nice little flags you can use, and it tends to be a bit easier
> to read than using brackets all over the place. Of course, everyone
> has their own preferences and there are probably 4-5 ways to
> accomplish the same thing.

> For example, imagine the following if statement:

>    if [[ "`oslevel | cut -f1 -d'.'`" = "4" ]]

> Then rewrite it this way and it still works the same:

>    if test "$(oslevel | cut -f1 -d'.')" = "4"

> regards,

> Steve N.

Thanks Steve, the syntax "${ftpsever}"="aixprod" worked.

Jordan

 
 
 

string comparison

Post by Jorda » Thu, 09 Jan 2003 01:32:20


Actually I think I spoke a little too soon.

if test "${ftpserver}"="aixprod"
then
    print "print anything"
fi

The set -x output returns
...
test isg1uw=aixprod
print
...
test aixprod=aixprod
print
...

The if statement always evaluates to true, am I missing something?

Thanks again,

Jordan






> > > I'm new to Unix scripting and am struggling with what seems to be very
> > > simple, could someone please help stop my madness.
> > >     if "$ftpserver"=aixprod
> > >     then
> > >         print "print anything"
> > >     fi

> > Hi Jordan,

> > Nicholas already answered you on this, as it seems it is simply your
> > shell syntax that is causing a problem. Nick uses the double brackets
> > [[ like_this ]] around the test portion of your if statement. Many
> > people also right it as follows:

> > if test "${ftpserver}" = "aixprod"
> > then
> >    print "print anything"
> > fi

> > The "test" command is listed in the AIX Command Reference to show you
> > all the nice little flags you can use, and it tends to be a bit easier
> > to read than using brackets all over the place. Of course, everyone
> > has their own preferences and there are probably 4-5 ways to
> > accomplish the same thing.

> > For example, imagine the following if statement:

> >    if [[ "`oslevel | cut -f1 -d'.'`" = "4" ]]

> > Then rewrite it this way and it still works the same:

> >    if test "$(oslevel | cut -f1 -d'.')" = "4"

> > regards,

> > Steve N.

> Thanks Steve, the syntax "${ftpsever}"="aixprod" worked.

> Jordan

 
 
 

string comparison

Post by Jorda » Thu, 09 Jan 2003 05:45:07





> > Actually I think I spoke a little too soon.

> > if test "${ftpserver}"="aixprod"
> > then
> >     print "print anything"
> > fi

> > The set -x output returns
> > ...
> > test isg1uw=aixprod
> > print
> > ...
> > test aixprod=aixprod
> > print
> > ...

> > The if statement always evaluates to true, am I missing something?

> Spaces around the equals sign?  This isn't C, and you don't get
> to ignore spaces willy-nilly.  You'll note that in all of the
> provided examples, there is clearly white space separating
> each token in the command line.

> --
> Gary R. Hook / AIX PartnerWorld for Developers / These opinions are MINE
> ________________________________________________________________________

That was it!  Thanks to all for your help (and patience)
 
 
 

1. Bash string comparison not working

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

2. Linux login hangs

3. string comparison problem

4. Samba and RedHat 5.0

5. String Comparison

6. NIC p

7. Multiple String Comparison in Ksh.

8. Graphical PPP app?

9. string comparison not working

10. string comparisons

11. AWK string comparisons

12. question on string comparison in ksh93

13. String comparison in C Shell programming