How to compare a parameter to a string

Hi,

I have a parameter which is a string:
set parameter = "string"

I would like to compare it to various strings inside an IF conditional:

if ($parameter == "string") then
bla bla bla
endif

but it doesn't work, and I have no idea why.
Thanks in advance,
Shira. :slight_smile:

try this,

if ("$parameter" == "string") then
bla bla bla
endif

Hi,

I tried it as well, but it just doesn't work. :confused:

I wasn't sure which language you are using. Could you please tell me? Also when you say it doesn't work, is there any kind of error it's giving you?

Hi,

I'm using csh.
It doesn't give me an error message, it just calculates it as FALSE instead of TRUE and then goes to the "else" section.

Although I'm not too familiar with this shell, I would add in some echo statements to see what the value is exactly for $parameter. Maybe before the if statement, you could add something like:

echo "parameter = x${parameter}x"

What you want to see then is:

parameter = xstringx

This will rule out any spaces or hidden characters that might be in there.

ok, I discovered the problem, but it's very odd.
At the beginning of the script I defined parameter.
I echoed it right after the definition and it was okay.

I echoed it right before the if like you said, but it showed me that inside parameter, instead of string, there was 0!

It's as if parameter was changed throughout the script, but I didn't touch it until I got to the if part!

Do you have any idea why this thing could happen?

Thanks.

The fact that it was 0 is suspicious. Normally 0 indicates that a command was run successfully. Without seeing your code it's hard to tell but it leads me to wonder if somehow $parameter was reassigned by the result of a previous execution. It's just my guess though.

ok, I tried a little test, and I discovered that it changes parameter during the condition of the if. very strange.
Here's the code:

set parameter = ${3} #${3} is the string
echo "$parameter" #works fine, returns string.
if ($parameter == "string") then
    echo "match: $parameter"
else
    echo "doesn't match: $parameter"
endif

it returns doesn't match: 0
very odd.

It is odd. In fact I tested your script but I was getting a match. I wonder if you would get the same results by using $3 in your conditional instead of assigning it to 'parameter'. It would look like this:

echo "$3"
if ($3 == "string") then
    echo "match: $3"
else
    echo "doesn't match: $3"
endif

2pugs,

It worked, thanks so much, you're the best. :slight_smile:

Shira.

Try this

set parameter = ${3} #${3} is the string
echo "$parameter" #works fine, returns string.
if ("$parameter" == "string") then
    echo "match: $parameter"
else
    echo "doesn't match: $parameter"
endif

Hi frans,

I tried this solution, but it didn't work for me.

Thanks,
Shira.