gpt4 book ai didi

arrays - Bash:嵌套for循环+中间循环包含两个数组?

转载 作者:太空宇宙 更新时间:2023-11-04 10:15:48 24 4
gpt4 key购买 nike

也许我想多了,但这是我想要的输出:

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

关于arrays - Bash:嵌套for循环+中间循环包含两个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46315188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com