bash 在bash中同时迭代两个数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17403498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterate over two arrays simultaneously in bash
提问by user2354862
I have two arrays.
我有两个数组。
array=(
Vietnam
Germany
Argentina
)
array2=(
Asia
Europe
America
)
I want to loop over these two arrays simulataneously, i.e. invoke a command on the first elements of the two arrays, then invoke the same command on the second elements, and so on. Pseudocode:
我想同时循环遍历这两个数组,即对两个数组的第一个元素调用命令,然后对第二个元素调用相同的命令,依此类推。伪代码:
for c in $(array[*]}
do
echo -e " $c is in ......"
done
How can i do this?
我怎样才能做到这一点?
回答by gniourf_gniourf
From anishsane's answer and the comments therein we now know what you want. Here's the same thing in a bashierstyle, using a for loop. See the Looping Constructssection in the reference manual. I'm also using printf
instead of echo
.
从 anishsane 的回答和其中的评论中,我们现在知道您想要什么。这是使用 for 循环的bashier风格的相同内容。请参阅参考手册中的循环结构部分。我也在使用printf
而不是echo
.
#!/bin/bash
array=( "Vietnam" "Germany" "Argentina" )
array2=( "Asia" "Europe" "America" )
for ((i=0;i<${#array[@]};++i)); do
printf "%s is in %s\n" "${array[i]}" "${array2[i]}"
done
Another possibility would be to use an associative array:
另一种可能性是使用关联数组:
#!/bin/bash
declare -A continent
continent[Vietnam]=Asia
continent[Germany]=Europe
continent[Argentina]=America
for c in "${!continent[@]}"; do
printf "%s is in %s\n" "$c" "${continent[$c]}"
done
Depending on what you want to do, you might as well consider this second possibility. But note that you won't easily have control on the order the fields are shown in the second possibility (well, it's an associative array, so it's not really a surprise).
根据您想要做什么,您不妨考虑第二种可能性。但请注意,您不会轻易控制在第二种可能性中字段显示的顺序(好吧,它是一个关联数组,所以这并不奇怪)。
回答by cthomaspdx
If all of the arrays are ordered correctly just pass around the index.
如果所有数组都正确排序,只需传递索引。
array=(
Vietnam
Germany
Argentina
)
array2=(
Asia
Europe
America
)
for index in ${!array[*]}; do
echo "${array[$index]} is in ${array2[$index]}"
done
Vietnam is in Asia
Germany is in Europe
Argentina is in America
回答by anishsane
You need a loop over array & array2
您需要在 array & array2 上循环
i=0
while [ $i -lt ${#array[*]} ]; do
echo ${array[$i]} is in ${array2[$i]}
i=$(( $i + 1));
done
Vietnam is in Asia
Germany is in Europe
Argentina is in America
EDIT:Do not use the below tr
based implementation. It will not work for array elements containing spaces. Not removing it so as to keep the comments relevant. See glenn Hymanman's comment instead of below answer.
编辑:不要使用以下tr
基于实现。它不适用于包含空格的数组元素。不删除它以保持评论的相关性。请参阅 glenn Hymanman 的评论而不是下面的答案。
/EDIT
/编辑
Alternately, you can use this option (without loop):
或者,您可以使用此选项(不带循环):
paste <(tr ' ' '\n' <<< ${array[*]}) <(tr ' ' '\n' <<< ${array2[*]}) | sed 's/\t/ is in /'
回答by Fabrício Pereira
If the two vars were two string with multiple lines, like this:
如果两个变量是多行的两个字符串,如下所示:
listA=$(echo -e "Vietnam\nGermany\nArgentina")
listB=$(echo -e "Asia\nEurope\nAmerica")
Then, the solution for this case is:
那么,这种情况的解决方案是:
while read strA <&3 && read strB <&4; do
echo "$strA is in $strB"
done 3<<<"$listA" 4<<<"$listB"
回答by runlevel0
Specifically for the question asked (arrays with 3 items):
专门针对所问的问题(包含 3 个项目的数组):
for i in $(seq 0 2) ; do echo "${array1[$i]} is in ${array2[$i]}" ; done
for i in $(seq 0 2) ; do echo "${array1[$i]} is in ${array2[$i]}" ; done