I went through several links here in stackoverflow but nothing seems to work for me.
Here's my situation: I have a bash script where I echo some env variables and also have a array. I use this array to get some value based on a key, which is another env variable. I want to do the same to a set of remote servers, so I loop thru' all the remote servers and via ssh run these commands. The problem is that I'm not sure how to pass this array over so that the command on the remote server will use the key from its local env variable and get the value.
Here's my code -
declare -A arr
arr["key1]=val1
arr+=( ["key2"]=val2 ["key3"]=val3)
echo "$HOSTNAME, ${arr[${env1}]}"
for srv in $servers; dp
ssh -l user@pswd $srv 'echo $HOSTNAME, ${arr[${env1}]}' 2>/dev/null
done
I need single quotes since I want the all variables to be expanded on the remote server with the exception of $arr. How can i accomplish this?
Following the examples in Passing a variable to a remote host in a bash script with ssh and EOF doesn't work in my case.
Here's what I did:- replaced the $arr in double-quotes, but since the key within that map needs to be expanded/obtained from the remote server, this approach doesn't work.
declare -A arr
arr["key1]=val1
arr+=( ["key2"]=val2 ["key3"]=val3)
echo "$HOSTNAME, ${arr[${env1}]}"
for srv in $servers; dp
ssh -l user@pswd $srv 'echo $HOSTNAME, '"${arr[${env1}]}" 2>/dev/null
done