Sample Header Ad - 728x90

Indirect access in bash arrays

1 vote
3 answers
334 views
I am trying to do the following indirect task:
host_1=(192.168.0.100 user1 pass1)
host_2=(192.168.0.101 user2 pass2)

hostlist=( "host_1" "host_2" )

for item in ${hostlist[@]}; do

current_host_ip=${!item}
current_host_user=${!item}
current_host_pass=${!item}

echo "IP: $current_host_ip User: $current_host_user Pass: $current_host_pass"

done
I'm trying to understand how should I perform this indirect request so I pull the hostname from the array "hostlist", and then I should do indirect request to pull the host 1 IP, user and pass. But when I'm trying to do it, I'm stuck with either only first variable (only IP), or all variables inside one (if I add [@] into the end of variable name), empty result, or numbers from array. I can't understand how can I first copy the host_1 array into current_ variables and then (after my script does some work) I need to pass the host_2 variables to the same variables current_. Can you pinpoint my mistake? I think this is the solution to the problem I just can't adopt it: https://unix.stackexchange.com/questions/20171/indirect-return-of-all-elements-in-an-array
Asked by sxiii (9 rep)
Jul 17, 2020, 08:06 PM
Last activity: Jul 19, 2020, 01:56 AM