With zsh
:
if ((${#${(u)ARRAY_DISK_Quantity[@]}} == 1)); then
echo OK
else
echo not OK
fi
Where (u)
is a parameter expansion flag to expand unique values. So we're getting a count of the unique values in the array.
Replace == 1
with <= 1
is you want to consider an empty array is OK.
With ksh93
, you could sort the array and check that the first element is the same as the last:
set -s -- "${ARRAY_DISK_Quantity[@]}"
if [ "$1" = "${@: -1}" ]; then
echo OK
else
echo not OK
fi
With ksh88 or pdksh/mksh:
set -s -- "${ARRAY_DISK_Quantity[@]}"
if eval '[ "$1" = "${'"$#"'}" ]'; then
echo OK
else
echo not OK
fi
With bash
, you'd probably need a loop:
unique_values() {
typeset i
for i do
[ "$1" = "$i" ] || return 1
done
return 0
}
if unique_values "${ARRAY_DISK_Quantity[@]}"; then
echo OK
else
echo not OK
fi
(would work with all the Bourne-like shells with array support (ksh, zsh, bash, yash)).
Note that it returns OK for an empty array. Add a [ "$#" -gt 0 ] || return
at the start of the function if you don't want that.