簡體   English   中英

Bash:嵌套的for循環+中間的循環包含兩個數組?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM