Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if bash array values are all same

I have a bash array "RUN_Arr" with values as given below. If the values are same, I want the script to continue else I want to report them.

echo "${RUN_Arr[@]}"
"AHVY37BCXY" "AHVY37BCXY" "AHVY37BCXY" "AHVY38BCXY" "AHVY37BCXY" "AHVY37BCXY"

Based on the above array, I want to echo:

 No the array values are not same
 "AHVY37BCXY" "AHVY38BCXY"

Can someone suggest a solution? Thanks.

like image 962
Gopinath S Avatar asked Sep 20 '25 20:09

Gopinath S


1 Answers

Iterate through your array, and test against a watermark:

arr=(a a a b a a a)

watermark=${arr[0]}
for i in "${arr[@]}"; do
    if [[ "$watermark" != "$i" ]]; then
        not_equal=true
        break
    fi
done

[[ -n "$not_equal" ]] && echo "They are not equal ..."

Very simplistic Proof-Of-Concept for you; obviously harden as appropriate for your purposes.

like image 159
hunteke Avatar answered Sep 22 '25 10:09

hunteke