简体   繁体   中英

How to check if any array element is not holding a specific string

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    

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM