Hello I want to ask a question that is repeated here.
I have four servers in bash script defined like in the code below.
For each server, I want to maintain the ID of the process I have started on it.
Just for testing, I wanted to initialize each array with 10 20 30 40.
And see if I can access these elements as expected. However I cannot access the elements.
Could someone tell me what exactly I am doing wrong.
#!/bin/bash
SERVER_LIST=("server1" "server2" "server3")
for server in ${SERVER_LIST[@]} ; do
echo $server
arrayName=$server"process"
echo $arrayName
set -a "$arrayName=(10 20 30 40)"
done
current_sever=${SERVER_LIST[0]}
arrayName=$current_server"process"
# The attempt below is failing.
eval "echo Server ${current_server} \${$arrayName[*]}"
echo $(eval echo \${arrayName[*]})Server server1
server1process
It gives me output as follows –
Server server1
server1process
Can someone help please.
Also can you please tell me how to append new element to the array?
I tried the following, but it doesn’t work –
sleep 10 &
arrayName=$current_server"process"
eval "\${$arrayName[*]}+=$!"
Try replacing line 8:
with: