0

I'm trying to see if a value exists in a bash array. If so, do something. Else, do something else.

I can't figure out why this is always failing for me.

Basically start with two separate values, concatenate them with a delimiter, search against an array and if that entire string (delimiter and all) is in any of the array elements do something, else do something else.

#!/bin/bash

FNAME="JACK"
LNAME="BLACK"

SEARCHNAME=()
SEARCHNAME+="JACK-BLACK"
SEARCHNAME+="JOHN-JAMES"
SEARCHNAME+="CHRIS-TOPHER"
SEARCHNAME+="JEN-NAY"

NAME="${FNAME}-${LNAME}"

if [[ $NAME == *"${SEARCHNAME[@]}"* ]]; then
    echo "PASSED"
else
    echo "FAILED"
fi

echo "SEARCH IN: ${SEARCHNAME[@]}"
echo "FOR NAME: ${NAME}"

Results:

FAILED
SEARCH IN: JACK-BLACKJOHN-JAMESCHRIS-TOPHERJEN-NAY
FOR NAME: JACK-BLACK

I've also tried if [[ "${NAME}" == *"${SEARCHNAME[@]}"* ]] but no difference... I'm missing something pretty obvious here and am thinking it might be with the way I'm declaring my array? I'd expect the lack of space between each array element (seen in array output) to not make a difference because of the wild-card characters?

2
  • You are not adding array elements, easily checked by adding the line echo "There are ${#SEARCHNAME[@]} members of the array" at the end of your script. It will indicate 1 member. The += assignments should look like SEARCHNAME+=("JACK-BLACK").
    – Deathgrip
    Commented Jun 30, 2017 at 17:23
  • Thanks for pointing that out. I re-ran but it still is failing? #!/bin/bash FNAME="JACK" LNAME="BLACK" SEARCHNAME=() SEARCHNAME+=("JACK-BLACK") SEARCHNAME+=("JOHN-JAMES") SEARCHNAME+=("CHRIS-TOPHER") SEARCHNAME+=("JEN-NAY") NAME="${FNAME}-${LNAME}" if [[ "${NAME}" == *"${SEARCHNAME[@]}"* ]]; then echo "PASSED" else echo "FAILED" fi echo "SEARCH IN: ${SEARCHNAME[@]}" echo "FOR NAME: ${NAME}" echo "ELEMENT COUNT: ${#SEARCHNAME[@]}" Results FAILED SEARCH IN: JACK-BLACK JOHN-JAMES CHRIS-TOPHER JEN-NAY FOR NAME: JACK-BLACK ELEMENT COUNT: 4
    – Chris
    Commented Jun 30, 2017 at 17:28

2 Answers 2

2

Correct the array assignments, and change the test to a regex.

#!/bin/bash

FNAME="JACK"
LNAME="BLACK"

SEARCHNAME=()
SEARCHNAME+=("JACK-BLACK")
SEARCHNAME+=("JOHN-JAMES")
SEARCHNAME+=("CHRIS-TOPHER")
SEARCHNAME+=("JEN-NAY")

NAME="${FNAME}-${LNAME}"

if [[ "${SEARCHNAME[@]}" =~ $NAME ]]; then
    echo "PASSED"
else
    echo "FAILED"
fi

echo "SEARCH IN: ${SEARCHNAME[@]}"
echo "FOR NAME: ${NAME}"
2
  • Thanks for correcting my poor syntax! Also, I guess I've been using the comparitor wrong for a while now. If I leave it without regex, but swap the spots so it's if [[ "${SEARCHNAME[@]}" == *"${NAME}"* ]]; it works. Thanks for your help!
    – Chris
    Commented Jun 30, 2017 at 17:35
  • I'd still use the regex =~ method in my answer over the == test.
    – Deathgrip
    Commented Jun 30, 2017 at 17:39
1

There are a couple things to note here:

  1. Using the == in place of =~ as we"re not looking for equality here.
  2. Since this is a regex not wildcard so ...
  3. The order of operands in the [[ needs to be reversed, viz., like as:

    if [[ ${SEARCHNAME[@]} =~ .*$NAME.* ]]
    

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.