Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
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"
share|improve this question
    
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 – user454038 Dec 11 '15 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? – Andy Dalton Dec 11 '15 at 19:00
    
use while. for does one thing, while does anything while true. – mikeserv Dec 11 '15 at 19:09
    
Here is my master and slave cluster variable – user147074 Dec 11 '15 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" – user147074 Dec 11 '15 at 19:57

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
share|improve this answer
    
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. – user147074 Dec 11 '15 at 18:17

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
share|improve this answer
    
Here is my master and slave varibale – user147074 Dec 11 '15 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" – user147074 Dec 11 '15 at 20:01
    
I want to grab value of Master_Cluster1 with Slave_Cluster_1 in one for loop. – user147074 Dec 11 '15 at 20:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.