Bash根据索引循环分配变量命令输出

时间:2017-05-31 13:00:11

标签: bash

我有这样的事情:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "chrome",
            "request": "launch",
            "name": "Launch Chrome against localhost",
            "url": "http://localhost:4200",
            "webRoot": "${workspaceRoot}"
        },
        {
            "type":"chrome",
            "request": "launch",
            "name": "Launch Chrome against Karma",
            "url": "http://localhost:9876/?id=9792346",
            "webRoot": "${workspaceRoot}"
        }
    ]
}

我想在最后一个循环中,我不知道如何将i索引传递给#!/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 命令,然后将"$(docker-machine ip worker$i)"设置为此返回值。然后我不知道如何回显所有这些IP地址。

1 个答案:

答案 0 :(得分:2)

使用数组。

IPCache=()

for ((i = 0; i < NCACHES; i++))
do
    IPCache+=("$(docker-machine ip worker$i)")
    echo "IP worker$i=${IPCache[i]}"
done