How to check a list of 4-5 strings in IF condition?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check a list of 4-5 strings in IF condition?
# 1  
Old 09-29-2010
How to check a list of 4-5 strings in IF condition?

Hi Unix Champs,

I am a newbe to UNIX.

I need to create a condition where I want to check the value of a variable with 5 different strings, if matched then exit 1.

For ex:
Code:
if [[ $var=one or $var=two or $var=three or $var=four or $var=five ]]
then
       echo "yes, condition satisfied...."
else  
       echo "no, condition not satisfied..."
fi


Regards,
A beginner...

Moderator's Comments:
Mod Comment With so many posts you should be familiar using code tags. Use them in future please, ty.
# 2  
Old 09-29-2010
Code:
if [[ $var = "one" || $var = "two" || $var = "three" || $var = "four" || $var = "five" ]]
then
       echo "yes, condition satisfied...."
else  
       echo "no, condition not satisfied..."
fi


Last edited by zaxxon; 09-29-2010 at 11:37 AM.. Reason: added quotation marks.. and blanks between operators
# 3  
Old 09-29-2010
Hi....Thanks for the reply....
I tried with the code you suggested, but getting following error.....

ksh: 0403-057 Syntax error: `|' is not expected.
# 4  
Old 09-29-2010
Forgot to add double quotation marks when testing a string. Have been added in the post above. Oh and blanks between operators!
# 5  
Old 09-29-2010
still getting the same error.....checked with quotation marks for all 5 strings...
# 6  
Old 09-29-2010
use case and esac

Code:
case "$var" in
  one|two|three|four|five ) echo "yes";;
  *) echo "no";;
esac

# 7  
Old 09-29-2010
I was still editing, check my post. There are missing blanks between the operators and the " = ".
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

2. Shell Programming and Scripting

Check two condition in while loop

Hi, I Have to check two condition in while loop every 2 minutes. while loop is accompanied with number of times it will check.Please help in putting the two condition in while loop as appropriate. z= input value, A=1 while do 1.check the file output,if the file output is N then keep on... (2 Replies)
Discussion started by: netdbaind
2 Replies

3. Shell Programming and Scripting

Comparing two strings receiving form two different loops and execute if condition when single match

I want to read a file contain sub-string and same string need to match in file name I got from for loop. I am using below code: #!/bin/bash C_UPLOADEDSUFFIX='.uploaded' files=$(find . -iname '*'$C_UPLOADEDSUFFIX -type f) # find files having .uploaded prefix for file in $files do ... (1 Reply)
Discussion started by: ketanraut
1 Replies

4. UNIX for Dummies Questions & Answers

Condition check using awk

Hi, I have a file in the following format "SYLVESTER,WILLARD G"|"S00633600"|"221052958A"|"H2256"|"015"|""|"00000042BASJ"|"665303"|"N"|"20100211"|"380.4"|""|""|""|"5400"|"20110218"|""|"20110218"|"FEESC"|"D"|"F"|"P" "PURINGTON-KELLEY,C"|"S00808783"|"029424717A"|"H2256"|"024"|"MEMBER JOINED... (3 Replies)
Discussion started by: nua7
3 Replies

5. Shell Programming and Scripting

ksh: how to extract strings from each line based on a condition

Hi , I'm a newbie.Never worked on Unix before. I want a shell script to perform the following: I want to extract strings from each line ,based on the type of line(Nameline,Subline) and output it to another file.Below is a sample format. 2010-12-21 14:00"1"Nameline"Midterm"First Name:Jane ... (4 Replies)
Discussion started by: angie1234
4 Replies

6. Shell Programming and Scripting

Take a list if strings from a file and search them in a list of files and report them

I have a file 1.txt with the below contents. -----cat 1.txt----- 1234 5678 1256 1234 1247 ------------------- I have 3 more files in a folder -----ls -lrt------- A1.txt A2.txt A3.txt ------------------- The contents of those three files are similar format with different data values... (8 Replies)
Discussion started by: realspirituals
8 Replies

7. UNIX for Dummies Questions & Answers

Delete strings in file1 based on the list of strings in file2

Hello guys, should be a very easy questn for you: I need to delete strings in file1 based on the list of strings in file2. like file2: word1_word2_ word3_word5_ word3_word4_ word6_word7_ file1: word1_word2_otherwords..,word3_word5_others... (7 Replies)
Discussion started by: roussine
7 Replies

8. UNIX for Dummies Questions & Answers

Strings with Special chars in IF condition

I was trying to run a code to check if a fax number is empty or not. for that, I've written the following code which is throwing an error. #!/bin/ksh fax= "999-999-9999" if ; then fax_no="000-000-0000" else fax_no=$fax fi echo $fax_no And I get the... (7 Replies)
Discussion started by: hooaamai
7 Replies

9. Shell Programming and Scripting

WHILE LOOP CONDITION CHECK

Hello I want to compare values of two variables as CHECK condition in a while loop. eg: var1=0 var2=10 while do echo " $var1 " var1=`expr $var1 + 1` done However this is giving error.How to do it in a proper manner? Thanks. (3 Replies)
Discussion started by: dashing201
3 Replies
Login or Register to Ask a Question