bash shell 将字符串添加到数组

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

shell adding string to an array

arraysbashshell

提问by Pectus Excavatum

I am having some trouble adding a string to an array within a loop. For some reason it always adds the same line. Here is my code:

我在将字符串添加到循环中的数组时遇到了一些麻烦。出于某种原因,它总是添加相同的行。这是我的代码:

declare -a properties
counter=0

while read line
do

    if [[ ${line} == *=* ]]
    then
        properties[${counter}]=${line}
        (( counter=counter + 1 ))
    fi
done < ${FILE}

for x in ${!properties[@]}
do
    echo "the value is $properties[x]"
done

For some reason each element in the array is the first line in the file. I must be doing something wrong, just not sure what.

出于某种原因,数组中的每个元素都是文件中的第一行。我一定是做错了什么,只是不确定是什么。

Any help would be greatly appreciated

任何帮助将不胜感激

回答by anubhava

Try this script:

试试这个脚本:

declare -a properties
while read line
do
   [[ "${line}" == *=* ]] && properties+=("$line")
done < "${FILE}"

for x in "${properties[@]}"
do
   echo "the value is "$x"
done

回答by Henk Langeveld

As @twalberg mentions in this comment, the problem is not in the top loop, but in the bottom one:

正如@twalberg在此评论中提到的,问题不在顶部循环中,而在底部循环中:

for x in ${!properties[@]}
do
    echo "the value is $properties[x]"
done

Array references alwaysneed braces { ... }to expand properly.

数组引用总是需要大括号{ ... }才能正确扩展。

For some reason each element in the array is the first line in the file.

出于某种原因,数组中的每个元素都是文件中的第一行。

Not so. The array is correctly populated, but you need to change the reference to the array from:

不是这样。数组已正确填充,但您需要更改对数组的引用:

echo "the value is $properties[x]"

to:

到:

echo "the value is ${properties[x]}"

Just a simple oversight.

只是一个简单的疏忽。

回答by DopeGhoti

A much simpler way to add an element to an array is to simply use the syntax:

将元素添加到数组的一种更简单的方法是简单地使用语法:

VARNAME+=("content")

Also, as written, your bug may be here:

此外,正如所写,您的错误可能在这里:

(( counter=counter + 1 ))

It probably should be one of these three:

它可能应该是这三个之一:

(( counter=$counter + 1 ))
counter+=1
counter=$[$counter+1]
counter=$(($counter + 1))

回答by javaPlease42

This KornShell (ksh) script worked fine for me. Let me know if anything.

这个 KornShell (ksh) 脚本对我来说很好用。让我知道如果有的话。

readFileArrayExample.ksh

读取文件数组示例.ksh

#! /usr/bin/ksh

file=input.txt
typeset -i counter=0

while read line
do
    if [[ ${line} == *=* ]]; then
        properties[${counter}]="${line}"
        ((counter = counter + 1))
        echo "counter:${counter}"
    fi
done < "${file}"

#echo ${properties[*]}

for x in "${properties[@]}"
do
    echo "${x}"
done

readFileArrayExample.ksh Output:

readFileArrayExample.ksh 输出:

@:/tmp #ksh readFileArrayExample.ksh
counter:1
counter:2
counter:3
a=b
a=1
b=1
@:/tmp #

input.txt

输入.txt

a-b
a+b
a=b
a=1
b=1
1-a