0

Is there a way of checking all the elements in an array, and if all the elements in the array are not holding a string mystring to return true? For example, if any element of an array with 2 elements is holding mystring I want it to make it return as false, anything else is true:

[mystring][mystring] = false/don't do anything
[mystring][A] = false/don't do anything
[@#$2][mystring]=false/don't do anything
[asda][wrwe]=true

Q: How can check an array with n elements, if none of the elements within that array hold any other value other then mystring it should return true?

My attempt was:

    for element_number in `seq 0 $going_through_the_elements_of_the_array`;
    do
          my_var=${the_array[$element_number]}


                if ! [[ $my_var == "$my_string" ]]
                then
                     echo " This should be printed"
                     exit
                fi
    done    

1 Answer 1

1

This should do what you want:

case ${the_array[@]}
in
  *my string*) echo "true" ;;
  *) echo "false" ;;
esac

It expands the array into a single string then uses the RE mechanism in the case statement to search for your target. The case where it is found prints true, all other cases print false.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.