If a string variable value exists in a set of values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If a string variable value exists in a set of values
# 1  
Old 03-26-2014
If a string variable value exists in a set of values

Can some one please help me with the syntax in shell script for the below :
Code:
if $var1 exists in ('val1','val2','val3')

I want to execute a set of commands if the value of var1 variable matches any one of the given string values. Please let me know if there are any other option to go by.

Thanks

Last edited by Pandee; 03-26-2014 at 10:56 AM.. Reason: code tags please
# 2  
Old 03-26-2014
Either use grep:
Code:
if echo 'val1 val2 val3' | grep -q "$var1" ; then
   echo $var1 found
fi

or case:
Code:
case "$var1" in
   val1|val2|val3) echo $var1 found ;;
esac

This User Gave Thanks to Subbeh For This Post:
# 3  
Old 03-26-2014
I would go with Subbeh's second option, the case statement.

There are two reasons for this, because the echo....grep ...:-
  1. creates extra processes, so in a loop, this could be costly to the elapse time
  2. might make false matches
To illustrate the latter, consider this:-
Code:
var1="a"
if echo 'val1 val2 val3' | grep -q "$var1" ; then
   echo Match found
fi



Robin

Last edited by rbatte1; 03-26-2014 at 11:14 AM.. Reason: Clarity
This User Gave Thanks to rbatte1 For This Post:
# 4  
Old 03-26-2014
Code:
$ var1="a"
$ if echo 'val1 val2 val3' | grep -wq "$var1" ; then
>    echo Match found
> fi
$

This User Gave Thanks to anbu23 For This Post:
# 5  
Old 03-26-2014
I tried the below command :

Code:
        case "$var1" in abc|pqs|dfgg|dfsf
                echo The queue is $Que
        ;;
        esac

It gave the below error :

Code:
syntax error near unexpected token `newline'

Also tried the below command :

Code:
$ var1="a"
$ if echo 'val1 val2 val3' | grep -wq "$var1" ; then
>    echo Match found
> fi

The condition is not getting satisfied. It is executing the commands for all the values of $var1

Please help me.

Last edited by vbe; 03-26-2014 at 12:56 PM.. Reason: code tags not ICODE!! please
# 6  
Old 03-26-2014
That is not how case works, reread the example shown to you.

Code:
case "$var1" in
   val1|val2|val3) echo $var1 found ;;
esac

Variable is in red, expression is in green.

The newlines, ), and ;; are not optional. That's exactly how it needs to look.
These 2 Users Gave Thanks to Corona688 For This Post:
# 7  
Old 03-26-2014
Thanks for your reply Corona.

I made the changes as you had pointed out. The error got cleared. Still the condition is not getting satisfied. Below are the exact set of scripts that I'm using :

Code:
        case "$var1" in ABCD|ASDF|WEDF|YUIO)
                echo The queue is $var1
        ;;
        esac

The value of $var1 keeps on changing in a for loop. When it's value is either of 'ABCD' or 'ASDF' or 'WEDF' or 'YUIO', I want certain action to be performed. The value of $var1 is picked up with awk command (from each line of a file)

Please do let me know if I'm doing anything wrong here.

Last edited by vbe; 03-26-2014 at 01:10 PM.. Reason: code tags please not ICODE
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check if 2 input values exists in a file

I have a file number.txt.I need to get 2 inputs from the terminal like a=100 and b=200.If a and b are there in the file,then check if a < b,print "less".If a is not there in the file,print "a is missing" or if b is not there in the file,print "b is missing". number.txt: 100 200 300 (2 Replies)
Discussion started by: aneeta13
2 Replies

2. Programming

[LUA] Set variables according to if file exists

Heya I'm using Awesome WM with the Vicious widget library. As i'm using multi boot, Win8, Fedora and Arch, i have my WM-Config shared accross my GNU/Linux installations. The regarding snippet: -- Functions -- Its just a workaround for an 'unstable' 'hwmon/hwmon' definition of Fedora21 -... (0 Replies)
Discussion started by: sea
0 Replies

3. Shell Programming and Scripting

How to get value from array and set those values as a variable

I am new to ksh scripting, specially array. How do i get values from an array and set the value as variable and pass those variables to the different functions?? someone taught me how to get input from a file with have columns i need to read, but now i doesnt know how to set those value to be a... (7 Replies)
Discussion started by: gavin_L
7 Replies

4. Shell Programming and Scripting

ENQUIRY WHETHER SCRIPT FOR DETECTING WORDS FROM A SET OF LETTERS EXISTS

Hello, I am interested in finding out whether someone has a perl or awk script which takes a set of letters such as wak and referring to a dictionary spews out all possible forms such as awk, kaw etc. If someone has such a script, could it be put up please. The script should handle Unicode. Many... (0 Replies)
Discussion started by: gimley
0 Replies

5. Shell Programming and Scripting

Test if string exists

How do I use bash to test whether a string occurs more than two times in a file? (2 Replies)
Discussion started by: locoroco
2 Replies

6. Shell Programming and Scripting

Assigning a set of values to a variable

I wnat to assign a set of values to a variable and use it in if condition. for example: i=$1 d=1 2 3 4 5 6 if then echo "Fine" else echo "Check" fi i will either of the value in d, i.e. i can be 1 or 2 or any value in d, How this can be done? Thanks in advance (2 Replies)
Discussion started by: sol_nov
2 Replies

7. Shell Programming and Scripting

BASH - set specific user variable via string operators

Apologies for the utter triviality of this question, but we all have to start somewhere! I've also tried searching but this question is pretty vague so I didn't (a) really know what to search for or (b) get many relevant hits to what I did search for. Anyway, I'm in the process of self-teaching... (1 Reply)
Discussion started by: u5j84
1 Replies

8. Shell Programming and Scripting

Howto set string variable in TCL?

Anyone knows how to set variable in TCL ? let say i have a password variable and it will have different values. set variable and variable has different values like: xxxx yyyy zzzz (0 Replies)
Discussion started by: linuxgeek
0 Replies

9. UNIX for Dummies Questions & Answers

set sub-string as variable

Hi all, i really new in linux and just heard about shell scripting couple days ago.. i did exercises on linux in online tutorial but as a beginner, i'm facing problems in developing the script as there are errors that sometimes i dun have any idea on how to solve it.What i'm doing now is not... (3 Replies)
Discussion started by: tedy2808
3 Replies

10. Shell Programming and Scripting

COMPARING STRING VALUES TO A VARIABLE and return code dignostics

Hey Folks and Gurus there I am trying to replicate this logic in bash : (1) if ; then function1 parameters & function2 parameters & fi Is there an easy method esp one with regex that can do this comparision. e.g. $var is compared to this list ( case regardless ) (... (1 Reply)
Discussion started by: sieger007
1 Replies
Login or Register to Ask a Question