Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check if any list item match variable

The goal is to verify if the string matches any of list item.

This is opposite to CHECK IF VARIABLE EXISTS in the list

Code should be similar to:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

email="john.brown@google.com"
list=["app-1", "app-2", "john"]

if [ $email -contains $list]; then
  echo "email contains variable from list"
else
  echo "email does not contain any of list items"
fi

# Item1: false -> email does not contains "app-1"
# Item2: false -> email does not contains "app-2"
# Item3: true  -> email contains "john"

>Solution :

Try with:

#!/bin/bash

email="john.brown@google.com"
list=("app-1" "app-2" "john")

found=false

for item in "${list[@]}"; do
  if [[ $email == *"$item"* ]]; then
    echo "Found!"
    found=true
    break
  fi
done

if [ $found == false ]; then
  echo "Not found."
fi
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading