Computing.Net > Forums > Unix > comparing strings in unix

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

comparing strings in unix

Reply to Message Icon

Name: kuku
Date: June 10, 2002 at 07:09:34 Pacific
Comment:

Does anyonw know how to compare two strings to see if they match, i want to do the following...

if string1 is not equal to string2
then
echo "ERROR"
fi

it would be even better if i can compare mutilple strings in one line,

if string1 is not equal to (string2, string3, string4)
then
echo "ERROR"
fi



Sponsored Link
Ads by Google

Response Number 1
Name: Joy Schoenberger
Date: June 10, 2002 at 07:45:30 Pacific
Reply:

Try this:

if ( $string1 != $string2 ) then
echo "ERROR"
fi


0

Response Number 2
Name: LANkrypt0
Date: June 10, 2002 at 07:48:32 Pacific
Reply:

If these strings are defined as variables its pretty easy: lets start out with these variables

a=1
b=1
c=1
d=2

If we do:
#!/bin/ksh
if [[ $a -ne $b ]];then
echo Error
else
echo Good
fi

We would get "Good"
But if we had:
#!/bin/ksh
if [[ $a -ne $d ]];then
echo Error
else
echo Good
fi

We would get "Error"

To compare multiple strings use the "OR" function (||):

#!/bin/ksh
if [[ $a -ne $b || $a -ne $c ]];then
echo Error
else
echo Good
fi

We get "Good" becuase $a is equal to both.

If we do
#!/bin/ksh
if [[ $a -ne $b || $a -ne $d ]];then
echo Error
else
echo Good
fi

We get error because we are saying:
If a is not equal to b OR if a is not equal to d, then echo error.
Since a=1 and d=4 and are not equal the script gives the error.

NOTE: When using the OR (||) you need to use the above syntax.

Using:
if [[ $a -ne $b || $c || $d ]];then
etc
fi

That will not work.


0

Response Number 3
Name: kuku
Date: June 10, 2002 at 09:36:30 Pacific
Reply:

that doesnt seem to work for strings...


0

Response Number 4
Name: kuku
Date: June 10, 2002 at 09:46:23 Pacific
Reply:

Got it working, thanks


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: comparing strings in unix

How 2 find java script used in UNIX www.computing.net/answers/unix/how-2-find-java-script-used-in-unix/8124.html

how to read binary files in Unix www.computing.net/answers/unix/how-to-read-binary-files-in-unix/4566.html

Length of file names in Unix www.computing.net/answers/unix/length-of-file-names-in-unix/2186.html