-1

in my question i learn to use loops and with you help I final a installation script for multible instance of a software. thank you very much :)

now I try to automatic setup the configuration files by using sed. For this i need multible variables in a loop.

I read from the system the IP-Adresses and the hostnames for the IP's (PTR)

IPADDR=`ifconfig | awk '{print $2}' | egrep -o '([0-9]+\.){3}[0-9]+'|grep -v 127.0.0.1`
for ipaddr in ${IPADDR[@]}; do echo $ipaddr; done
for iphost in ${IPADDR[@]}; do host $iphost |grep pointer | awk '{print $NF}' RS='.\n'; done

my Script know, there ar 3 IP's, know the IP-Addresses and the Hostnames.

the numbers of IP (3) are now my 001 002 003. this running well.

if I like to edit the config files with sed, I need the 3 variable to do this.

command anyname-001 -some -parameter in my case is a copy to a path. my path is now

/etc/anyname-001, /etc/anyname-003 and /etc/anyname-003

by using sed I need also the 3 IP-Addresses and the 3 hostnames.

sed -i 's/IPADDR/'${ipaddr}'/g' /etc/anyname-${number}/config.cfg
sed -i 's/HOSTNAME/'${hostname}'/g' /etc/anyname-${number}/config.cfg

how can I bring my loop to this with all variables on same time. T try many things. I found nested loops but it not work

001 >> IP:a.a.a.a >> hostname aaa.aaa.aa

002 >> IP:b.b.b.b >> hostname bbb.bbb.bb

003 >> IP:c.c.c.c >> hostname ccc.ccc.cc

Thank you

Community
  • 1
  • 1
nbx
  • 1
  • 2
  • It's conceivable that you might have a question here that's actually on-topic, but right now it's a bit buried, and definitely isn't reproducible by anyone else (since to run your code, we would need to have a system with the same DNS configuration and IP addresses). If the question this is closed as duplicative of doesn't help, please try to create a [Minimal, Complete, Verifiable Example](http://stackoverflow.com/help/mcve) -- code someone else can run to see your problem even if they don't have identical IP/host/interface configuration. – Charles Duffy Sep 26 '16 at 16:48
  • If you're having trouble figuring out how to rewrite the question so other people can test their answers, consider trying to create a test case for your underlying bash question that has nothing to do with hostnames, IP addresses, etc -- there are plenty of ways to exercise `for` loops, after all; doing so will also make your bash-language question much more clear, by separating it from a larger context you've only partially described. If you do such a rewrite, @-notify me, and I'll reverse the close-as-dupe. – Charles Duffy Sep 26 '16 at 16:49

1 Answers1

0

Assuming the length of both of your arrays HOSTNAME and IPADDR are the same, then you can loop over their elements via indexes in one run.

The length of an array is calculated by using the '#' in the array variable, for example as:

echo ${#HOSTNAME[@]}

So, overall your code would look like something:

count=${#HOSTNAME[@]}
for (( i=0; i<${count}; i++ ));
do
  echo ${HOSTNAME[$i]};
  echo ${IPADDR[$i]};
  ((j=i+1));
  sed -i 's/IPADDR/'${IPADDR[$i]}'/g' /etc/anyname-${j}/config.cfg
  sed -i 's/HOSTNAME/'${HOSTNAME[$i]}'/g' /etc/anyname-${j}/config.cfg
done
sal
  • 3,190
  • 1
  • 8
  • 19
  • i think my problem is, $HOSTNAMES is no a array. there are only one name inside :/ – nbx Sep 26 '16 at 16:07
  • Then you need to figure that out first. Once you do, the code above should work or give you at least an example of the way to iterate over multiple arrays in one loop. – sal Sep 26 '16 at 16:19