0

I have something like this:

#!/bin/bash

#numero di nodi cache della edge network
NCACHES=$1

#creo vm manager (dello swarm) e balancer
docker-machine create -d virtualbox manager
docker-machine create -d virtualbox balancer

#creo le restanti NCACHES-1 VM
for i in {0..NCACHES-1}
do 
    echo "Creating VM $i"
    docker-machine create -d virtualbox worker$i
done

docker-machine create -d virtualbox backend

IPManager="$(docker-machine ip manager)"
echo "IP VM swarm manager=$IPManager"

IPBalancer="$(docker-machine ip balancer)"
echo "IP VM balancer=$IPBalancer"

for i in {0..NCACHES-1}}
do
    IPCache$i="$(docker-machine ip worker$i)"
    echo "IP worker$i=IPCache$i"
done

I want that in the last loop, i don't know how to pass i index to the "$(docker-machine ip worker$i)" command, and then set IPCache$i to this returned value. Then i don't know how to echo all these IP addresses.

6
  • 1
    Why don't you make IPCache an array? Then you can have just one IPCache variable and set IPCache[$i] Commented May 31, 2017 at 13:04
  • You're looking for variable indirection, but I second Charles Duffy's suggestion to use an array instead Commented May 31, 2017 at 13:04
  • 1
    for i in {0..NCACHES-1} will never work! brace expansion will happen before variable expansion Commented May 31, 2017 at 13:05
  • You didn't notice a problem with for i in {0..NCACHES-1}? Commented May 31, 2017 at 13:05
  • @Aaron, grumble re: suggesting the ABS as a reference -- it's the W3Schools of bash, full of outdated information and bad-practice examples. I'd strongly suggest the BashFAQ #6 section on indirect assignment instead. Commented May 31, 2017 at 13:05

1 Answer 1

1

Use an array.

IPCache=()

for ((i = 0; i < NCACHES; i++))
do
    IPCache+=("$(docker-machine ip worker$i)")
    echo "IP worker$i=${IPCache[i]}"
done
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.