Comparing 2 variables in UNIX

Hi,

I have 2 variables as given below. How can i compare them and say its matching ? Appreciate your help

VAR1=describe/read/write
VAR2=read/write/describe

Thanks,

Please become accustomed to post - at least - your OS and shell versions.
Comparing variables is done quite differently in the various tools / languages. So an answer to your question is impossible at the current level of information.

Am using 3.10.0-957.1.3.el7.x86_64 and ksh shell

Thanks,

And, what is required of the contents of the two variables to determine that they match?

If VAR3 had been set with:

VAR3=abcddeeeeiirrrstw//

would it match? It does contain every character that is in the other two variables and contains each of them the same number of times.

And, what about:

VAR4=abcdeirstw/

? It contains every character that is in the other three variables, but only has one occurrence of each of those characters.

Does case matter?

VAR5=Read/wRiTe/DeScRiBe

What have you tried to solve this on your own?

In addition to knowing which version of an operating system you're using, it would also help to know what operating system you're using? And, what version of ksh are you using?

You could try something like this quick and dirty approach:

#!/bin/ksh
# similar.sh

ifs_old="${IFS}"
IFS="/"

VAR1=describe/read/write
VAR2=read/write/describe

VAR3=( ${VAR1} )
VAR4=( ${VAR2} )
count=1

for string in ${VAR3[0]} ${VAR3[1]} ${VAR3[2]}
do
    for n in 0 1 2
    do
        if [ "${VAR4[${n}]}" = "${string}" ]
        then
            if [ ${count} -eq 3 ]
            then
                echo "Two variables, VAR1 and VAR2 are similar!"
            fi
            count=$(( count+1 ))
        fi
    done
done

IFS="${ifs_old}"
1 Like