2016-08-02 227 views
0

我想在我的for循環中讀取分區和複製因子作爲參數我該怎麼做?而不是2和3,我想從rf中讀取並分割原始好壞的相應值。在shell中讀取for循環中的多個參數

topics=(raw good bad) 
rf=(4 6 8) 
partition=(2 3 4) 
for topic in ${topics[*]} 
do 
    bin/kafka-topics.sh --create --replication-factor 2 --partitions 3 --topic "$topic" --zookeeper $zk_hosts 
done 
+0

要清楚,它會,例如' - 複製因子4 - 部分2 - 主題原始'和' - 複製因子6 - 部分3 - 主題好'? – chepner

回答

2

聽起來你需要一個數字環路:

for ((i = 0; i < ${#topics[@]}; ++i)); do 
    # whatever you want with the corresponding elements of each array, e.g. 
    echo "${topics[i]} ${rf[i]} ${partition[i]}" 
done 

循環計數器從0到陣列topics的長度,所以這是假定每個其它陣列具有相同數量的元素。