-1
for SLAVE_CLUSTER,MASTER_CLUSTER in $MASTER_CLUSTERS $SLAVE_CLUSTERS
do
      echo "Master cluster boxes are ${!MASTER_CLUSTER}"
      echo "Slave cluster boxes are ${!SLAVE_CLUSTER}"
done

I am trying to get value of SLAVE_CLUSTER,MASTER_CLUSTER in one for loop but i am getting errors. How can we get both variables in one for loop?


Here is my master and slave cluster variable

export MASTER_CLUSTER_1="MASTER1 MASTER2"
echo "MASTER_CLUSTER_1 = $MASTER_CLUSTER_1"
export MASTER_CLUSTER_2="MASTER1 MASTER2"
echo "MASTER_CLUSTER_2 = $MASTER_CLUSTER_2"
export SLAVE_CLUSTER_1="SLAVE1 SLAVE2 SLAVE3 SLAVE4"
echo "SLAVE_CLUSTER_1 = $SLAVE_CLUSTER_1"
export SLAVE_CLUSTER_2="SLAVE1 SLAVE2 SLAVE3 SLAVE4"
echo "SLAVE_CLUSTER_2 = $SLAVE_CLUSTER_2"
7
  • What does $MASTER_CLUSTER contain? What does $SLAVE_CLUSTER contain? (sample values are ok) If you are not listing box names as in @rcjohnson answer where there is a list of master boxes, and a separate list of slave cluster box names, then what is the desired way of listing that you want to see? And update the original question with this info if you can, thanks Commented Dec 11, 2015 at 18:44
  • Are you looking for every permutation of the contents of MASTER_CLUSTERS/SLAVE_CLUSTERS or a mapping from one to the other based on position? Commented Dec 11, 2015 at 19:00
  • use while. for does one thing, while does anything while true. Commented Dec 11, 2015 at 19:09
  • Here is my master and slave cluster variable Commented Dec 11, 2015 at 19:57
  • export MASTER_CLUSTER_1="MASTER1 MASTER2" echo "MASTER_CLUSTER_1 = $MASTER_CLUSTER_1" export MASTER_CLUSTER_2="MASTER1 MASTER2" echo "MASTER_CLUSTER_2 = $MASTER_CLUSTER_2" export SLAVE_CLUSTER_1="SLAVE1 SLAVE2 SLAVE3 SLAVE4" echo "SLAVE_CLUSTER_1 = $SLAVE_CLUSTER_1" export SLAVE_CLUSTER_2="SLAVE1 SLAVE2 SLAVE3 SLAVE4" echo "SLAVE_CLUSTER_2 = $SLAVE_CLUSTER_2" Commented Dec 11, 2015 at 19:57

2 Answers 2

0

I am not certain I understand your need for a single loop. You could get the same output using two consecutive loops like this

for MASTER_CLUSTER in $MASTER_CLUSTERS

    do
          echo "Master cluster boxes are ${!MASTER_CLUSTER}"
    done

for SLAVE_CLUSTER in $SLAVE_CLUSTERS
    do
          echo "Slave cluster boxes are ${!SLAVE_CLUSTER}"
    done

or if the values must be alternated then

for MASTER_CLUSTER in $MASTER_CLUSTERS
    do 
    echo "Master cluster boxes are ${!MASTER_CLUSTER}"
    for SLAVE_CLUSTER in $SLAVE_CLUSTERS
       do
          echo "Slave cluster boxes are ${!SLAVE_CLUSTER}"
       done
    done
1
  • No THis will not be helpful. I have to grab Master and Slave cluster values in single loop only I have to do parallel operation for Master and slave boxes. Commented Dec 11, 2015 at 18:17
0

Extending my answer based on the OP's clarification. Again, you can use an array:

$ cat /tmp/foo.sh
#/bin/bash

# Sample values from OP
export MASTER_CLUSTER_1="MASTER1 MASTER2"
export MASTER_CLUSTER_2="MASTER3 MASTER4" # (edited to be unique)
export SLAVE_CLUSTER_1="SLAVE1 SLAVE2 SLAVE3 SLAVE4"
export SLAVE_CLUSTER_2="SLAVE5 SLAVE6 SLAVE7 SLAVE8" # (edited to be unique)

# Create two arrays, one for masters and one for slaves.  Each array has
# two elements -- strings containing space delimited hosts
declare -a master_array=( "${MASTER_CLUSTER_1}" "${MASTER_CLUSTER_2}" )
declare -a slave_array=( "${SLAVE_CLUSTER_1}" "${SLAVE_CLUSTER_2}" )

# For this to work, both arrays need to have the same number of elements
if [[ "${#master_array[@]}" == ${#slave_array[@]} ]]; then
    for ((i = 0; i < ${#master_array[@]}; ++i)); do
        echo "master: ${master_array[$i]}, slave: ${slave_array[$i]}"
    done
fi

Sample output:

$ bash /tmp/foo.sh
master: MASTER1 MASTER2, slave: SLAVE1 SLAVE2 SLAVE3 SLAVE4
master: MASTER3 MASTER4, slave: SLAVE5 SLAVE6 SLAVE7 SLAVE8
3
  • Here is my master and slave varibale Commented Dec 11, 2015 at 20:00
  • export MASTER_CLUSTER_1="MASTER1 MASTER2" echo "MASTER_CLUSTER_1 = $MASTER_CLUSTER_1" export MASTER_CLUSTER_2="MASTER1 MASTER2" echo "MASTER_CLUSTER_2 = $MASTER_CLUSTER_2" export SLAVE_CLUSTER_1="SLAVE1 SLAVE2 SLAVE3 SLAVE4" echo "SLAVE_CLUSTER_1 = $SLAVE_CLUSTER_1" export SLAVE_CLUSTER_2="SLAVE1 SLAVE2 SLAVE3 SLAVE4" echo "SLAVE_CLUSTER_2 = $SLAVE_CLUSTER_2" Commented Dec 11, 2015 at 20:01
  • I want to grab value of Master_Cluster1 with Slave_Cluster_1 in one for loop. Commented Dec 11, 2015 at 20:01

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.