Bash中的可变列表或数组结构?我怎么能轻易地把它附加上去呢?

时间:2021-09-10 21:41:45

I'm trying to collect string values in a bash script. What's the simplest way that I can append string values to a list or array structure such that I can echo them out at the end?

我正在尝试在bash脚本中收集字符串值。将字符串值附加到列表或数组结构的最简单方法是什么,以便在末尾将它们回显?

6 个解决方案

#1


77  

$ arr=(1 2 3)
$ arr+=(4)
$ echo ${arr[@]}
1 2 3 4

Since Bash uses sparse arrays, you shouldn't use the element count ${#arr} as an index. You can however, get an array of indices like this:

由于Bash使用稀疏数组,所以不应该使用元素count ${#arr}作为索引。但是,你可以得到这样的索引数组:

$ indices=(${!arr[@]})

#2


9  

foo=(a b c)
foo=("${foo[@]}" d)
for i in "${foo[@]}"; do echo "$i" ; done

#3


2  

The rather obscure syntax for appending to the end of an array in bash is:

在bash中,附加到数组末尾的相当模糊的语法是:

myarr[${#myarr[*]}]=”$newitem”

#4


2  

To add to what Ignacio has suggested in another answer:

为了增加伊格纳西奥在另一个答案中的建议:

foo=(a b c)
foo=("${foo[@]}" d) # push element 'd'

foo[${#foo[*]}]="e" # push element 'e'

for i in "${foo[@]}"; do echo "$i" ; done

#5


1  

$ for i in "string1" "string2" "string3"
> do
> array+=($i)
> done
$ echo ${array[@]}
string1 string2 string3

#6


0  

Though the question is answered and is pretty old, I'd like to share a namespace-solution as it works significantly faster than any other ways except for ennukiller's answer (on my 100k lines tests it won ~12 secs against my ~14 secs, whereas list-append solution would take a few minutes).

尽管这个问题已经得到了回答,而且已经很老了,但我还是想分享一个名称空间解决方案,因为它比除ennukiller的答案之外的任何方法都快得多(在我的100k行测试中,它比我的~14秒赢得了大约12秒,而list-append解决方案只需要几分钟)。

You can use the following trick:

你可以使用以下技巧:

# WORKS FASTER THAN THESE LAME LISTS! ! !
size=0;while IFS= read -r line; do
    echo $line
    ((++size))
    eval "SWAMP_$size='$line'"
done

Or you can do the following:

或者你可以这样做:

#!/bin/bash
size=0
namespace="SWAMP"

ArrayAppend() {
    namespace="$1"
    # suppose array size is global
    new_value="$2"
    eval "${namespace}_$size='$2'"
    eval "echo \$${namespace}_$size"
    ((++size))
}

ArrayAppend "$namespace" "$RANDOM"
ArrayAppend "$namespace" "$RANDOM"
ArrayAppend "$namespace" "$RANDOM"
ArrayAppend "$namespace" "$RANDOM"
ArrayAppend "$namespace" "$RANDOM"

As long as the interpreter is in tag list, here's a link for object oriented bash.

只要解释器在标记列表中,这里就有一个面向对象bash的链接。

#1


77  

$ arr=(1 2 3)
$ arr+=(4)
$ echo ${arr[@]}
1 2 3 4

Since Bash uses sparse arrays, you shouldn't use the element count ${#arr} as an index. You can however, get an array of indices like this:

由于Bash使用稀疏数组,所以不应该使用元素count ${#arr}作为索引。但是,你可以得到这样的索引数组:

$ indices=(${!arr[@]})

#2


9  

foo=(a b c)
foo=("${foo[@]}" d)
for i in "${foo[@]}"; do echo "$i" ; done

#3


2  

The rather obscure syntax for appending to the end of an array in bash is:

在bash中,附加到数组末尾的相当模糊的语法是:

myarr[${#myarr[*]}]=”$newitem”

#4


2  

To add to what Ignacio has suggested in another answer:

为了增加伊格纳西奥在另一个答案中的建议:

foo=(a b c)
foo=("${foo[@]}" d) # push element 'd'

foo[${#foo[*]}]="e" # push element 'e'

for i in "${foo[@]}"; do echo "$i" ; done

#5


1  

$ for i in "string1" "string2" "string3"
> do
> array+=($i)
> done
$ echo ${array[@]}
string1 string2 string3

#6


0  

Though the question is answered and is pretty old, I'd like to share a namespace-solution as it works significantly faster than any other ways except for ennukiller's answer (on my 100k lines tests it won ~12 secs against my ~14 secs, whereas list-append solution would take a few minutes).

尽管这个问题已经得到了回答,而且已经很老了,但我还是想分享一个名称空间解决方案,因为它比除ennukiller的答案之外的任何方法都快得多(在我的100k行测试中,它比我的~14秒赢得了大约12秒,而list-append解决方案只需要几分钟)。

You can use the following trick:

你可以使用以下技巧:

# WORKS FASTER THAN THESE LAME LISTS! ! !
size=0;while IFS= read -r line; do
    echo $line
    ((++size))
    eval "SWAMP_$size='$line'"
done

Or you can do the following:

或者你可以这样做:

#!/bin/bash
size=0
namespace="SWAMP"

ArrayAppend() {
    namespace="$1"
    # suppose array size is global
    new_value="$2"
    eval "${namespace}_$size='$2'"
    eval "echo \$${namespace}_$size"
    ((++size))
}

ArrayAppend "$namespace" "$RANDOM"
ArrayAppend "$namespace" "$RANDOM"
ArrayAppend "$namespace" "$RANDOM"
ArrayAppend "$namespace" "$RANDOM"
ArrayAppend "$namespace" "$RANDOM"

As long as the interpreter is in tag list, here's a link for object oriented bash.

只要解释器在标记列表中,这里就有一个面向对象bash的链接。