String Comparison

String Comparison

Post by Cai, Ben [WOLL:4009:EXCH » Wed, 14 Mar 2001 08:35:58



Hi all,
Simple question, how do I compare two strings in the bourne shell to
tell which one comes alphabetically first?

Logic I'm looking for is

if (string1 < string2)
    string1 comes first
else
    string2 comes first
fi

Is there an easy way to do this?

 
 
 

String Comparison

Post by Peter Sundstro » Wed, 14 Mar 2001 09:51:52




Quote:> Hi all,
> Simple question, how do I compare two strings in the bourne shell to
> tell which one comes alphabetically first?

> Logic I'm looking for is

> if (string1 < string2)
>     string1 comes first
> else
>     string2 comes first
> fi

> Is there an easy way to do this?

This is a classic SAQ.

Solution is for ksh/bash

#!/bin/ksh
string1='abc'
string2='def'

if [[ $string1 < $string2 ]]
then
   echo $string1 comes first
else
   echo $string2 comes first
fi

 
 
 

String Comparison

Post by t.. » Wed, 14 Mar 2001 17:58:24



Quote:> Simple question, how do I compare two strings in the bourne shell to
> tell which one comes alphabetically first?

> Logic I'm looking for is

> if (string1 < string2)
>     string1 comes first
> else
>     string2 comes first
> fi

> Is there an easy way to do this?

Try

if expr string1 \< string2
then
  # string1 comes first
else
  # string2 comes first
fi

--
Tapani Tarvainen

 
 
 

String Comparison

Post by Joe Halpi » Wed, 14 Mar 2001 22:42:43



Quote:> Hi all,
> Simple question, how do I compare two strings in the bourne shell to
> tell which one comes alphabetically first?

There's probably a better way, but here's one possibility.

#!/bin/sh
varlist=`echo "xyz\nabc\n" | sort`
set $varlist
echo $1 comes before $2

Joe

 
 
 

String Comparison

Post by Friedrich Dominicu » Wed, 14 Mar 2001 23:29:42



Quote:> Hi all,
> Simple question, how do I compare two strings in the bourne shell to
> tell which one comes alphabetically first?

> Logic I'm looking for is

> if (string1 < string2)
>     string1 comes first
> else
>     string2 comes first
> fi

> Is there an easy way to do this?

While using Scsh  (Scheme Shell (at least a Shell ;-)
(define str1 "abc")
(define str2 "def")
(if (string< str1 str2)
      (display "str1 comes first\n")
      (display "str2. comes first\n"))

life could be easy ;-)

Regards
Friedrich

 
 
 

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. ELF Library Support (What is it?)

3. string comparison problem

4. Multiple IP Addresses

5. Multiple String Comparison in Ksh.

6. linux recvfrom bug?

7. string comparison not working

8. Kent Linux User Group - United Kingdom

9. string comparisons

10. string comparison

11. AWK string comparisons

12. question on string comparison in ksh93

13. String comparison in C Shell programming