5

I have a file config.ini with the following contents:

@ndbd

I want to replace @ndbd with some other text to finalize the file. Below is my bash script code:

ip_ndbd=(108.166.104.204 108.166.105.47 108.166.56.241)

ip_temp=""
for ip in $ip_ndbd
do
    ip_temp+="\n\[ndbd\]\nHostname=$ip\n"   
done
perl -0777 -i -pe "s/\@ndbd/$ip_temp/" /var/lib/mysql-cluster/config.ini

Basically, I just want to get all the ip addresses in a specific format, and then replace @ndbd with the generated substring.

However, my for loop doesn't seem to be concatenating all the data from $ip_ndbd, just the first item in the list.

So instead of getting:

[ndbd]
HostName=108.166.104.204 

[ndbd]
HostName=108.166.105.47 

[ndbd]
HostName=108.166.56.241

I'm getting:

[ndbd]
HostName=108.166.104.204 

I'm pretty sure there's a better way to write this, but I don't know how.

I'd appreciate some assistance.

Thanks in advance.

3 Answers 3

12

If you want to iterate over an array variable, you need to specify the whole array:

ip_ndbd=(108.166.104.204 108.166.105.47 108.166.56.241)

ip_temp=""
for ip in ${ip_ndbd[*]}
do
    ip_temp+="\n\[ndbd\]\nHostname=$ip\n"   
done
Sign up to request clarification or add additional context in comments.

Comments

2

Replace

ip_ndbd=(108.166.104.204 108.166.105.47 108.166.56.241)

with

ip_ndbd="108.166.104.204 108.166.105.47 108.166.56.241"

1 Comment

the array is better, you only have to cycle it in the right way.
0

i didn't see any usage of your file with content @ndbd...

is this what you want?

kent$  echo "108.166.104.204 108.166.105.47 108.166.56.241"|awk '{for(i=1;i<=NF;i++){print "[ndbd]";print "HostName="$i;print ""}}'
[ndbd]
HostName=108.166.104.204

[ndbd]
HostName=108.166.105.47

[ndbd]
HostName=108.166.56.241

you could just redirect the output to your config.ini file by > config.ini

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.