I'm trying to match the first three characters of a specific hostname to a key in an associative array. And then print the matching value. The code I have below prints all the values in the array.
!/bin/bash
host=$(hostname | cut -c1-3)
declare -A dc
dc=(
[loc]="http://loc.example.com"
[vr1]="http://vr1.example.com"
)
for K in "${!dc[@]}"
do if [ $host == $K ];
then echo "${dc[@]}"
fi
done
echo "${dc[@]}"
byecho "${dc[$K]}"
? – steeldriver Feb 20 '21 at 19:46