So in order to be versatile, I make a lot of functions and i want to define my own variable name when i call on that function. For basic text, numbers it works and this is how I do it
function func_get_device_hostname
{
local _device_hostname=$1
local device_hostname2="TEST"
eval $_device_hostname="'$device_hostname2'"
}
function func_display_device_hostname
{
func_get_device_hostname device_hostname
echo $device_hostname
}
but when i use that same method with an array like so
function func_get_vds_ip
{
func_get_vds_count vds_count
local _arr_vds_ip=$1
arr_vds_ip2=()
i=1
while [ $i -le $vds_count ]; do
local arr_vds_ip2+=($(avahi-browse -rvpc vds._tcp | grep "=;" | cut -d ';' -f8 | sed -n $i' p'))
i=$(($i+1))
done
echo ""
eval $_arr_vds_ip="'$arr_vds_ip2'"
}
function func_display_vds_ip
{
func_get_vds_ip arr_vds_ip
echo -n "VDS IP "
for i in "${arr_vds_ip[@]}"
do
echo -n "$i"
echo -n " - "
done
}
I get the following error
line 339: {arr_vds_ip[@]}=192.168.18.82: command not found
line 339 refers to this part of the code
eval $_arr_vds_ip="'$arr_vds_ip2'"
what is the best method to do this? am i using a wrong command?