string comparisons

string comparisons

Post by Clinton Ca » Sat, 09 Apr 1994 12:03:32



I am writing a small script at home for use at work,  I have Linux at home
Sun OS 4.1.3 at work.  I am writing a bash/sh script which needs to compare
two string variables.  My "UNIX in a Nutshell" book hints to a syntax of [[]]
but I haven't figured it out.  Can the following example be implemented in
bash/sh?

ws> testscript abcd

#!/bin/bash -
for arg
do
        if [ $arg < "abc" ]; then
        ...
        else
        if [ $arg > "abcz" ]; then
        ...
        fi
done

                        thanks

 
 
 

string comparisons

Post by Chet Ram » Sun, 10 Apr 1994 04:08:42



>Can the following example be implemented in bash/sh?

Not using test/[, no.
--
"You can watch an actor absolutely sabotage a good script and then read
 reviews like 'Unfortunately, even the impressive talents of Cheech Marin
 could not salvage Anton Chekhov's trite and meandering script.'"


 
 
 

string comparisons

Post by Ray Jon » Sun, 10 Apr 1994 04:11:53



Quote:>I am writing a small script at home for use at work,  I have Linux at home
>Sun OS 4.1.3 at work.  I am writing a bash/sh script which needs to compare
>two string variables.  My "UNIX in a Nutshell" book hints to a syntax of [[]]
>but I haven't figured it out.  Can the following example be implemented in
>bash/sh?
>ws> testscript abcd
>#!/bin/bash -
>for arg
>do
>    if [ $arg < "abc" ]; then
>    ...
>    else
>    if [ $arg > "abcz" ]; then
>    ...
>    fi
>done

The [] construct is just another way of using the "test" function of the
shell.  In fact, if you look in /bin you will find a file named "[" which is
linked to "test".  From the man page on test;
    s1=s2     True if strings s1 and s2 are identical.

    s1!=s2    True if strings s1 and s2 are not identical.

    s1        True if s1 is not the null string.

Therefore,
  if [ "$arg"="abc"] ; then #means, if $arg is identical to abc do the next
statement.
 if test "$arg"="abc"
is the same statement.

See the man page on test for more information.

 
 
 

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. ODBC

3. string comparison problem

4. sendmail.cf file

5. String Comparison

6. Optimal memory for RH 7.1

7. Multiple String Comparison in Ksh.

8. 1024 cylinder limit info please?

9. string comparison not working

10. string comparison

11. AWK string comparisons

12. question on string comparison in ksh93

13. String comparison in C Shell programming