[英]Bash: nested for loop + middle loop contains two arrays?
也許我對此考慮過多,但這是我想要的輸出:
one four seven
one five eight
one six nine
two four seven
two five eight
two six nine
three four seven
three five eight
three six nine
這是我開始的。 我走到第二個for循環,完全迷失了尋找解決方案的念頭。
#!/bin/bash
declare -a aaa=("four" "five" "six")
declare -a bbb=("one" "two" "three")
declare -a ccc=("seven" "eight" "nine")
for bs in ${bbb[@]}; do
for as in ${aaa[@]}, cs in ${ccc[@]}; do
echo "$bs" "$as" "$cs"
done
done
for
子句中不能有多個in
。
如果需要同時迭代兩個數組,請遍歷它們的索引:
#! /bin/bash
declare -a aaa=("four" "five" "six")
declare -a bbb=("one" "two" "three")
declare -a ccc=("seven" "eight" "nine")
for b in "${bbb[@]}" ; do
for i in "${!aaa[@]}" ; do # or ccc
echo "$b" "${aaa[i]}" "${ccc[i]}"
done
done
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.