How to compare a parameter to a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to compare a parameter to a string
# 1  
Old 01-09-2010
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. Smilie
# 2  
Old 01-09-2010
try this,

if ("$parameter" == "string") then
bla bla bla
endif
# 3  
Old 01-09-2010
Hi,

I tried it as well, but it just doesn't work. :/
# 4  
Old 01-09-2010
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?
# 5  
Old 01-09-2010
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.
# 6  
Old 01-09-2010
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:

Code:
echo "parameter = x${parameter}x"

What you want to see then is:
Code:
parameter = xstringx

This will rule out any spaces or hidden characters that might be in there.
# 7  
Old 01-09-2010
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. Shell Programming and Scripting

Passing string as a input parameter for a shell script

Hi i have a shell script which needs a string as an input parameter. How to pass the string param as an input? In command line am running the script. for e.g., a="who is a buddy?" sh sample.sh $a Inside the script i get this input param as $1 but only the value "who" is accepted... (12 Replies)
Discussion started by: vidhyaS
12 Replies

3. Shell Programming and Scripting

Check parameter is number or string

Hey I'm new in linux, I'm looking for a code to check whether the parameter is a number or a string. I have already tried this code: eerste=$(echo $1 | grep "^*$">aux) if But it doesn't work.:confused: Thanks (2 Replies)
Discussion started by: Eclecticaa
2 Replies

4. Shell Programming and Scripting

How to pass string as a parameter in a script

Hi friends. i am newbie to shell scripting. I need to create a script where i will be passing 2 parameters to the script and based on that it should work. For ex: start_proc a 2 or start_proc b 2 start_proc a 2 --- this should bring up 2 processes as i define inside the script. start_proc... (2 Replies)
Discussion started by: friscouser
2 Replies

5. Shell Programming and Scripting

String compare

Hi, I have file like below, Srinivas Jala Srinivas Jala AA Srikanth ML Srikanth ML KK Vijay Kumar Dha Vijay Kumar Dha JJ i want to compare like "Srinivas Jala" word in same line, if i found i shoud get some like found, or not found. Pls help to get the code. (3 Replies)
Discussion started by: Srinivas.Jala
3 Replies

6. Shell Programming and Scripting

How to compare a command line parameter with -- in shell scripting

Hi, I need to check if a parameter provided at the command line is equal to --.How can i do that ? Please help me. Thanks and Regards, Padmini (4 Replies)
Discussion started by: padmisri
4 Replies

7. UNIX for Dummies Questions & Answers

How to compare a string with IP

Hi, I have a variable with value tmp2=123.45.175.243, I am taking this value from a network file. In the script I need to check whether the variable has only numerals and .(dot). if ..." ] then printf "SUCCESS\n" else printf "FAILED\n" fi doesnt work, is there a alternate... (1 Reply)
Discussion started by: happyrain
1 Replies

8. Shell Programming and Scripting

Check if parameter passes in contains certain string

Hi Guys, I am writing a Unix script which accepts a directory path as parameter $1 so something like /user5.data/WA/01 will be passed in. I want to determine if the directory path passed in contains "WA" as above (because then I need to do something specific if it does) What is the... (9 Replies)
Discussion started by: bcunney
9 Replies

9. Shell Programming and Scripting

replacing a string in a file with command line parameter

Hello, I am trying to replace a string with a paramter given along with the script. I am replacing application1 to application2 with the script: ./change_app.sh application2 change_app.sh: #!/bin/ksh grep $1 applications.dat 2>&1 >/dev/null echo $1 file=pckage.new sed 's/Name:... (5 Replies)
Discussion started by: chiru_h
5 Replies

10. Shell Programming and Scripting

Passing a string parameter to a function

I need to pass a parameter to a function in a script. My parameter is a string. When I display the parameter within my function, I only get the first word from string I pass in. How can I make the function receive the whole string (and not terminate at the first space it encounters)?. part of... (1 Reply)
Discussion started by: fastgoon
1 Replies
Login or Register to Ask a Question