0

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[*]}+=$!"
2

1 Answer 1

1

Try replacing line 8:

set -a "$arrayName=(10 20 30 40)"

with:

eval "$arrayName=(10 20 30 40)"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.