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
# 8  
Old 01-09-2010
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.
# 9  
Old 01-09-2010
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:

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.

# 10  
Old 01-09-2010
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:

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

# 11  
Old 01-09-2010
2pugs,

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

Shira.
# 12  
Old 01-09-2010
Try this
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

# 13  
Old 01-09-2010
Hi frans,

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

Thanks,
Shira.
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