Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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"
fiit 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

If these strings are defined as variables its pretty easy: lets start out with these variables
a=1
b=1
c=1
d=2If we do:
#!/bin/ksh
if [[ $a -ne $b ]];then
echo Error
else
echo Good
fiWe would get "Good"
But if we had:
#!/bin/ksh
if [[ $a -ne $d ]];then
echo Error
else
echo Good
fiWe 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
fiWe 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
fiWe 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
fiThat will not work.

![]() |
![]() |
![]() |

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