0

I want to read a variable's data and see if any of it's comma-separated data matches.

Something like:

allowed_versions={v1.0,v1.1,v1.5,v2.3}
read -r -p 'Enter a version: ' version_inputted
if [[ "${version_inputted}" == "${allowed_versions}" ]]
then
  echo 'That version is allowed.'
else
  echo 'That version is NOT allowed.'
fi

So, in practice, it would look like:

Enter a version: v1.0
That version is allowed.

Enter a version: v9.6
That version is NOT allowed.

Enter a version: v1.5
That version is allowed.

Enter a version: asdflj';~!@ #$%^&*)_+=-<>,.?/~`\|}{][:"(
That version is NOT allowed.

Using bash 3.2.57(1)-release on OSX El Capitan 10.11.6.

I really prefer to use a oneliner for the variable, too, if possible. Thanks.

leetbacoon
  • 744
  • 5
  • 19
  • Any reason not use arrays? Also are `{v1.0` and `v2.3}` really a valid version? The `{` `}` have no special meaning in bash there, they are taken literally there. – KamilCuk Jun 19 '19 at 06:06
  • Kamil Cuk Pardon my lack of knowledge but I don't know what an array is. I assume it's a collection of values like `{......}`? – leetbacoon Jun 19 '19 at 06:17
  • 1
    @leetbacoon: Change your `allowed_versions` to an array in `bash` as `allowed_versions=(v1.0 v1.1 v1.5 v2.3)` and use the answer in the provided duplicate – Inian Jun 19 '19 at 06:27
  • 1
    @leetbacoon : No need for arrays here, if you define your versions between comma instead of curlys, i.e. `allowed_versions=,v1.0,v1.1,v1.5,v2.3,`, you can then do a `if [[ $allowed_versions == *,"$version_inputted",* ]]`. – user1934428 Jun 19 '19 at 06:49
  • 1
    If you really want to use the format given (and none of the allowed versions contain ",", "{", or "}"), you could use `if [[ "$allowed_versions" = *[{,]${version_inputted}[,}]* ]]` – Gordon Davisson Jun 19 '19 at 06:51
  • 1
    @Inian : This is not a duplicate question, because the OP doesn't ask of arrays, and arrays are not needed here. – user1934428 Jun 19 '19 at 06:52
  • @user1934428: I don't think so, OP wanted to use a bash's equivalent of list storage (array) and incorrectly used a syntax for a variable (refer first few comments). – Inian Jun 19 '19 at 06:53
  • @GordonDavisson : You don't have to quote `$allowed_versions` but you have to quote `$version_inputted`, to catch the case that someone inputs the string `v1*5`. – user1934428 Jun 19 '19 at 06:55
  • @Inian : He never asks about _list storage_. He just has a bunch of versions as a string, which reprsents a list of values in *a single variable*, as he explicitly says, and wants to do a pattern match. Why forcing him to use arrays? I don't see any advantage for arrays here. – user1934428 Jun 19 '19 at 07:01
  • Is it possible for me to replace the `,`s with `/`s instead and leave the rest of either user1934428's or Gordon Davisson's code in-tact? – leetbacoon Jun 19 '19 at 07:02
  • 2
    Re-opening the post as there lots of contradicts to the closure – Inian Jun 19 '19 at 07:02
  • @user1934428 Oops. Normally, I prefer to double-quote variables unless there's a specific reason not to, rather than the reverse. Fewer mistakes that way. Except this time I missed one that should've been quoted... – Gordon Davisson Jun 19 '19 at 07:05
  • @leetbacoon : My first attempt was to say "no", because in globbing, a slash marks the end of a directory. However, I tried it out and inside a `[[ ... ]]`, it seems to work. – user1934428 Jun 19 '19 at 07:07
  • @user1934428 Would wrapping in quotes like `allowed_versions='/v1.0/v1.1/v1.5/v2.3/'` be safer? – leetbacoon Jun 19 '19 at 07:09
  • @leetbacoon : Still, Inian is right in that you should sooner or later get familiar with [arrays](https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_10_02.html), even if you don't need them now, because they have many good uses in bash. – user1934428 Jun 19 '19 at 07:10
  • @leetbacoon : Some people have the habit to wrap in quotes often, necessary or not, so that they don't forgert a case where it is necessary. In your case, you don't have spaces in your version string, so quoting does not change anything. – user1934428 Jun 19 '19 at 07:11
  • @user1934428 Got it, I will just stick with your original suggestion. Thank you and thank you Gordon Davisson, too! – leetbacoon Jun 19 '19 at 07:15
  • read this :https://stackoverflow.com/questions/8063228/how-do-i-check-if-a-variable-exists-in-a-list-in-bash – Christina Jacob Jun 19 '19 at 07:53
  • @GordonDavisson I just noticed this, in your `if [[ "$allowed_versions" = *[{,]${version_inputted}[,}]* ]]` answer, how come you use `[{,]` and `[,}]`? Why not just `,` or `","` or `','`? – leetbacoon Aug 01 '19 at 06:18
  • @leetbacoon The original format used `{` and `}` as delimiters at the ends, so it's needed to match those. If you used user1934428's suggestion to put commas at the beginning and end, the bracket expressions wouldn't be necessary. – Gordon Davisson Aug 01 '19 at 07:28
  • @GordonDavisson I get it, thank you for clearing that up! – leetbacoon Aug 01 '19 at 08:54

0 Answers0