matrizes de matrizes são realmente muito dolorosas no bash
host=ny113
for grp in "${SERVERGROUP[@]}"; do
ref="${grp}[@]"
for srv in "${!ref}"; do
if [[ $host == $srv ]]; then
echo $host is in $grp
break 2
fi
done
done
ny113 is in NY
O uso de matrizes associativas (bash versão 4) é outra abordagem:
declare -A grps
grps[CA]="ca126 ca546 ca 642"
grps[NY]="ny112 ny113 ny114"
for grp in "${!grps[@]}"; do
# all quotes and spaces are very deliberate in next command
if [[ " ${grps[$grp]} " == *" $host "* ]]; then
echo $host is in $grp
break
fi
done
Ou você pode criar um "mapa reverso"
declare -A host2group
for grp in "${SERVERGROUP[@]}"; do
ref="${grp}[@]"
for srv in "${!ref}"; do
host2group[$srv]=$grp
done
done
echo $host is in ${host2group[$host]}
ny113 is in NY