0
firstvar="PRIMARY"
secondvar="SECONDARY"
thirdvar="TERTIARY"

array=($firstvar $secondvar $thirdvar)
echo ${array[*]} //prints PRIMARY SECONDARY TERTIARY

I want to iterate over this array in my bash script, and check if the 3 elements (PRIMARY SECONDARY TERTIARY) are present in the array with no specific order. If yes, echo "Success. If not, echo Failed. What would be a good way to approach this?

EDIT: I forgot to mention that if an element(e.g. PRIMARY) is missing from the array, it should also print out Primary is missing. For a similar array:

array_health=($firsthealth $secondhealth $thirdhealth)

These 3 variables can have either 1 or something else I want to check whether these 3 variables in the array have value=1, how would I check that? They're not initialized as firsthealth=0 or firsthealth=1.

  • @dawg No. because that's the difference if arrays, I don't want that. I just want to check whether strings "PRIMARY" "SECONDARY" "TERTIARY" are in my array. – confusedreally Jan 18 '21 at 19:07
  • 2
    *echo ${array[\*]} | grep -w $firstvar | grep -w $secondvar | grep -qw $thirdvar && echo yes || echo no* – alecxs Jan 18 '21 at 19:10
  • 1
    @alecxs: Gives a wrong answer if `"PRIMARY SECONDARY"` is in the array. – choroba Jan 18 '21 at 19:17
  • @alecxs Will this also echo yes, if one of the elements in the array is "PRIMARY IS DOWN" and I'm using "grep -w "PRIMARY"? Like if the substring matches would it print yes or no? – confusedreally Jan 18 '21 at 19:17
  • the way you have declared array doesn't allow whitespaces - so no, just iterate over array and do a case check instead – alecxs Jan 18 '21 at 19:22
  • @alecxs I forgot to mention that if one of the elements in missing in the array, then it should print out which element is missing. How would I do that using your grep method – confusedreally Jan 18 '21 at 19:25
  • Don't ask two different questions in one question. It makes it hard for people to answer. Also, if you add a question later, it makes all the existing answers incomplete. – choroba Jan 18 '21 at 19:27
  • 1
    The answers at [Compare/Difference of two arrays in Bash](https://stackoverflow.com/q/2312762/8593689) should really do what you need. You can get the difference, and that difference would tell you which items were missing. – Shane Bishop Jan 18 '21 at 19:28
  • @choroba right, didn't see whitespaces in original question – alecxs Jan 18 '21 at 19:53

3 Answers3

1

You can loop over the elements one by one:

flag=0
for ele in "$firstvar" "$secondvar" "$thirdvar"; do
    if [[ ! " ${array[@]} " =~ " ${ele} " ]]; then
        echo "$ele" is missing
        flag=1
    fi  
done    


if [[ flag -eq 0 ]]; then 
    echo "all there"
fi

If there is a possibility that your elements may have spaces, that will make the test above unreliable. You can use a different delimiter by using printf to add left and right delimiters to characters unlikely to be in your strings:

firstvar="PRIMARY SECONDARY"
secondvar="SECONDARY"
thirdvar="TERTIARY"

array=("$firstvar" "$thirdvar")

flag=0
printf -v tgt "|%s|" "${array[@]}"  # |ele1||ele2||eleN|
for ele in "$firstvar" "$secondvar" "$thirdvar"; do
    if [[ ! "$tgt" =~ "|${ele}|" ]]; then
        echo "$ele" is missing
        flag=1
    fi  
done    

if [[ flag -eq 0 ]]; then 
    echo "all there"
fi

Prints SECONDARY is missing

dawg
  • 98,345
  • 23
  • 131
  • 206
1

Try bash rematch

[[ ${array[@]} =~ $firstvar ]] && [[ ${array[@]} =~ $secondvar ]] && [[ ${array[@]} =~ $thirdvar ]] && echo ok || echo ko

Then like this

for item in $firstvar $secondvar $thirdvar; { 
    [[ ${array[@]} =~ $item ]] && echo "$item" || echo "$item is missing"
}
Ivan
  • 6,188
  • 1
  • 16
  • 23
1

beginner friendly simple loop with simple case counting all vars
followed by test concatenation and simple if statement

array=("$firstvar" "$secondvar" "$thirdvar")

for i in "${array[@]}"
  do
    echo "$i"
    case "$i" in
      "$firstvar")
        first=$((first+1))
      ;;
      "$secondvar")
        second=$((second+1))
      ;;
     "$thirdvar")
        third=$((third+1))
      ;;
    esac
done

if [ "$first" ] && [ "$second" ] && [ "$third" ]
  then
    echo "Success."
  else
    [ -z "$first" ] && echo "'$firstvar' missing"
    [ -z "$second" ] && echo "'$secondvar' missing"
    [ -z "$third" ] && echo "'$thirdvar' missing"
fi

regarding your second array basically same, quote your vars
(although it's unclear to me how you want preserve labels as you only have values in array)

array_health=("$firsthealth" "$secondhealth" "$thirdhealth")

uninitialized vars can be test'ed with -z (as above) or have default value with string manipulation

[ -z "$i" ] && echo "${i:-0}"
alecxs
  • 701
  • 8
  • 17
  • Does this answer your question? [Multi-dimensional arrays in Bash](https://stackoverflow.com/questions/11233825/multi-dimensional-arrays-in-bash) – alecxs Jan 18 '21 at 21:20