0

I need to check if the value c exists in an integer array, I'm aware of how to approach this using for loops and if statements:

    for c in {1..100};do
        sequence=(2 4 6 8 10 12 14 16)    
        for value in "${sequence[@]}";do
           if [[ $value -eq $c ]];then  #If c exists in sequence
              flag=1
              break
           fi
    done

done

But I don't want this, I tried something similar to this:

[[ ${sequence[*]} =~ $c ]]

But it didn't give me the desired results, I think it works only with string arrays, not integers.

How could I approach this?

  • 1
    In both solutions, you use a variable `c` without showing us its content. How can we one diagnose what is going on? The first solution is clearly wrong, because how can a test for equality reveal that a string consists of digits only? The second solution using a regular expression is more promising, provided that you use the correct regex (which you don't show us), but the left side can't be an array of course, but only an array element. – user1934428 Apr 01 '20 at 06:34
  • Ok so I edited the question, the first solution works well for me. – Somayyah Mohammed Apr 01 '20 at 06:39
  • It would fail if your sequence contains a value of, say, 1000000000, and it can not be reasonably extended to catch this case. Technically, your solution just checks in an expensive way if all elements in the sequence are integers in a certain range, which is different from your original requirement. – user1934428 Apr 01 '20 at 06:45
  • My sequence is predefined as the one above and c is always in the range {1..100}, that's why I said it works. – Somayyah Mohammed Apr 01 '20 at 07:02
  • I don't have to think about such cases. – Somayyah Mohammed Apr 01 '20 at 07:03
  • 2
    Try this: `grep -qFx $c – anubhava Apr 01 '20 at 07:15
  • Does this answer your question? [Check if a Bash array contains a value](https://stackoverflow.com/questions/3685970/check-if-a-bash-array-contains-a-value) – kvantour Apr 01 '20 at 07:51
  • Your proposed test contains a tiny problem which does not cover the cases at the beginning and end. You notice that with `${a[@]}` all elements are separated by a space. So you just need to add a space before and ofter your search term and the array: `[[ " ${array[@]} " =~ " $c " ]]` – kvantour Apr 01 '20 at 07:53

2 Answers2

0

Converting my comment to answer so that solution is easy to find for future visitors.

You may use this grep + printf solution:

grep -qFx "$c" <(printf '%s\n' "${sequence[@]}") && echo "found" || echo "nope"
anubhava
  • 664,788
  • 59
  • 469
  • 547
0

The problem with this method [[ ${sequence[*]} =~ $c ]] is that if $c is 1, than it'll match all instances with 1. Try this approach, make your sequence a regex like this

re=${sequence[*]}
re=${re// /|}
$ echo $re
2|4|6|8|10|12|14|16

Testing

c=1
$ [[ $c =~ $re ]] && echo ok || echo fail
fail

c=11
$ [[ $c =~ $re ]] && echo ok || echo fail
fail

c=12
$ [[ $c =~ $re ]] && echo ok || echo fail
ok
Ivan
  • 3,695
  • 1
  • 10
  • 13