Operating System - HP-UX
1821051 Members
2746 Online
109631 Solutions
New Discussion юеВ

comparing 2 string variables in a script

 
SOLVED
Go to solution

comparing 2 string variables in a script

I'm trying to compare 2 character string variables in a Korn Shell script without success:

#!/bin/ksh
testid=$(id $1)
echo $testid
prodid=$(remsh epicprod -n "id $1")
echo $prodid
if [[ $prodid = $testid ]]
then
echo "uid's are the same on both systems"
fi

There must be something wrong with my '$prodid = $testid' expression because even though the earlier echoes show identical character strings, the expression does not see them as equivalent.
What am I missing?
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

Re: comparing 2 string variables in a script

Hi:

Quote each variable used in the test command:

if [[ "$prodid" = "$testid" ]]

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: comparing 2 string variables in a script

Hi (again) Randolph:

And here's a simple test to show you the reason:

# X="a b c";Y="a b c";[ $X = $Y ] && echo same || echo differ
ksh: b: 0403-012 A test command parameter is not valid.
differ

# X="a b c";Y="a b c";[ "$X" = "$Y" ] && echo same || echo differ

You need to compare one string to one string, not multiple ones.

Regards!

...JRF...

Re: comparing 2 string variables in a script

Solution provided in replies.
Dennis Handly
Acclaimed Contributor

Re: comparing 2 string variables in a script

>if [[ "$prodid" = "$testid" ]]

Note: This is NOT comparing strings, this is doing pattern matching.
It might be better to use the tried and true:
if [ "$prodid" = "$testid" ]