Linux Unix bash shell 脚本 - 在“for”循环中迭代数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20337258/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:30:44  来源:igfitidea点击:

Unix bash shell script - Iterating an array in a 'for' loop

linuxbashshellunix

提问by ziggy

I have the following test script:

我有以下测试脚本:

#!/bin/sh

testArray=(A,B,C,D,E)
currentValue=''
tempValue=x

for i in "${testArray[@]}"
    do
        currentValue=$i

        echo "Processing " ${currentValue}

        if [ ${currentValue}==A ]
        then
            tempValue="$i 123"
        else
            tempValue=$i
        fi

        echo "Current loop " ${tempValue} 
        echo `date`

    done

When i test it, the output that i get is

当我测试它时,我得到的输出是

Processing  A,B,C,D,E
Current loop  A,B,C,D,E 123
Mon Dec 2 20:33:26 GMT 2013

It looks like the 'for' loop in Bash works somehow differently to what i am used to as i was expecting the following output (i.e. whatever is in the 'for' loop to be repeated for each of the array elements)

看起来 Bash 中的“for”循环的工作方式与我习惯的方式有所不同,因为我期待以下输出(即“for”循环中的任何内容都将针对每个数组元素重复)

Processing  A
Current loop  A 123
Mon Dec 2 20:29:44 GMT 2013

Processing  B
Current loop  B
Mon Dec 2 20:29:45 GMT 2013

Processing  C
Current loop  C
Mon Dec 2 20:29:46 GMT 2013

Processing  D
Current loop  D
Mon Dec 2 20:29:47 GMT 2013

Processing  E
Current loop  E
Mon Dec 2 20:29:48 GMT 2013
  • Why is the 123 at the end?
  • Why is the date command executed only once and not for each iteration
  • What do i do to make each iteration work correctly.
  • 为什么最后是123?
  • 为什么 date 命令只执行一次而不是每次迭代
  • 我该怎么做才能使每次迭代正常工作。

Basically what i am trying to achieve is to write a script that iterates through an array list and execute the same command based on different parameters dependent on the value of the current item in the array. I wrote the above script to try and understand how the for loop works but i am not getting the output i was expecting.

基本上我想要实现的是编写一个脚本,该脚本遍历数组列表并根据依赖于数组中当前项的值的不同参数执行相同的命令。我编写了上面的脚本来尝试了解 for 循环的工作原理,但我没有得到我期望的输出。

采纳答案by chepner

This line

这条线

testArray=(A,B,C,D,E)

creates an array with a single element, namely the string 'A,B,C,D,E'. Array elements are separated by whitespace, not commas. Use

创建一个包含单个元素的数组,即字符串 'A,B,C,D,E'。数组元素由空格分隔,而不是逗号。用

testArray=(A B C D E)


You'll also need to add whitespace to your ifstatement (and technically, you should use =inside [...], not ==, as well as quote the parameter expansion):

您还需要在if语句中添加空格(从技术上讲,您应该使用=inside [...]、 not==以及引用参数扩展):

if [ "${currentValue}" = A ]

回答by Chinmay Inamdar

One more way

另一种方式

Change your loop to:

将您的循环更改为:

for i in `echo ${testArray} | tr "," " "`

As Suggested by chepner Change conditional statement to:

根据chepner的建议将条件语句更改为:

if [ "${currentValue}" = A ]