12

I am currently trying to find a string within a variable that outputs something like this:

one,two,three

My code:

echo "please enter one,two or three) read var var1=one,two,threee if [[ "$var" == $var1 ]]; then echo "$var is in the list" else echo "$var is not in the list" fi 

EDIT2:

I tried this but still not matching. Yo uwere correct about it not matching the exact string from previous answers as it was matching partial.

 groups="$(aws iam list-groups --output text | awk '{print tolower($5)}' | sed '$!s/$/,/' | tr -d '\n')" echo "please enter data" read "var" if [ ",${var}," = *",${groups},"* ]; then echo "$var is in the list" else echo "$var is not in the list" fi 

Trying this its still not matching the exact string as i need it to.

6
  • what language is this? Commented May 31, 2018 at 18:56
  • bash script ... Commented May 31, 2018 at 18:59
  • 1
    can you add a tag 'bash', please? Commented May 31, 2018 at 19:02
  • how about? stackoverflow.com/questions/229551/string-contains-in-bash Commented May 31, 2018 at 19:04
  • my var1 is a command that runs and outputs data like i mentioned above. so i'm comparing $var to $var1 but it never successfully finds any strings i put from $var Commented May 31, 2018 at 19:10

3 Answers 3

3

There might be other problems (like matching partial words), but if you use =~ (match) instead of == it would work for simple cases

if [[ "$var1" =~ "$var" ]]; then ... 
Sign up to request clarification or add additional context in comments.

2 Comments

tried that , same thing. To give more context im using aws API to list IAM groups. var1=aws iam list-groups --output text | awk '{print $5}' | sed '$!s/$/,/ when i echo it from my script it shows up like this: admin, dev, stage
This worked, my mistake with a typo.
1

The other answers have a problem in that they'll treat matches of part of an item in the list as a match, e.g. if var1=admin,\ndev, \nstage(which I think is what you actually have), then it'll match ifvaris "e" or "min", or a bunch of other things. I'd recommend either removing the newlines from the variable (maybe by adding| tr -d '\n'` to the end of the pipeline that creates it), and using:

if [[ ",${var}," = *",${var1},"* ]]; then 

(The commas around $var anchor it to the beginning and end of an item, and the ones around $var1 allow it to word for the first and last items.) You could also make it work with the newlines left in $var1, but that's the sort of thing that'll mess you up sooner or later anyway.

1 Comment

Check my edit , i'm still unable to match the string out of the list. But your explanation that the others will match different scenarios is correct as that's what it was doing.
0

Something like this?

#!/bin/bash echo "please enter one,two or three" read var var1=["one","two","three"] if [[ ${var} = *${var1}* ]]; then echo "${var} is in the list" else echo "${var} is not in the list" fi 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.