0

I have an OR condition in a IF statement, what is the best way to do the following as I feel like its probably not best practice:

if [[ $name == "Mark" || $name == "Peter" || $name == "Alex" || $name == "Ben" || $name == "TerryCruise" ]]
code...
fi

Something equivalent to pythons "in" condition would be nice. If there is a way to create an array variable and just check if the names are in that, that'd be even better so that if in the future the names changed, the variable could just be changed instead of the if statement itself.

Any help would be appreciated. Thanks

Shak
  • 103
  • 8
  • Check if this blog helps ur requirements - https://www.unix.com/shell-programming-and-scripting/135234-if-condition-against-array-shell-script.html – Tim May 25 '21 at 05:35
  • Use `case` instead of multiple `||` – anubhava May 25 '21 at 05:46
  • @Shak : Of course the clean way would be `case`, as anubhava suggested, but in your concrete case, and if you don't mind its lack of elegance, a `if [[ ,Mark,Peter,Alex,Ben, == *,$name,* ]]; then ...` would do, although it would fail if `name` contains i.e. the string _Peter,Alex_. . For a more flexible solution, consider storing the set of allowed names into an array or a file and search that. – user1934428 May 25 '21 at 07:12

0 Answers0