如何在Bash中复制数组?

时间:2021-10-02 15:39:23

I have an array of applications, initialized like this:

我有一个应用程序数组,初始化如下:

depends=(cat ~/Depends.txt)

When I try to parse the list and copy it to a new array using,

当我尝试解析列表并将其复制到一个新的数组时,

for i in "${depends[@]}"
   if [ $i #isn't installed ]; then
      newDepends+=("$i")
   fi
done

What happens is that only the first element of depends winds up on newDepends.

所发生的是,只有依赖的第一个元素最终依赖于newdepend。

for i in "${newDepends[@]}"
   echo $i
done

^^ This would output just one thing. So I'm trying to figure out why my for loop is is only moving the first element. The whole list is originally on depends, so it's not that, but I'm all out of ideas.

^ ^这将只输出一件事。我想弄明白为什么for循环只移动第一个元素。整个列表最初都是依具体情况而定的,所以不是那样的,但是我的想法都没有了。

6 个解决方案

#1


69  

a=(foo bar "foo 1" "bar two")  #create an array
b=("${a[@]}")                  #copy the array in another one 

for value in "${b[@]}" ; do    #print the new array 
echo "$value" 
done   

#2


8  

The simplest way to copy a non-associative array in bash is to:

在bash中复制非关联数组的最简单方法是:

arrayClone=("${oldArray[@]}")

arrayClone = " $ { oldArray[@]}”)

or to add elements to a preexistent array:

或向先前存在的数组添加元素:

someArray+=("${oldArray[@]}")

someArray + =(“$ { oldArray[@]}”)

Newlines/spaces/IFS in the elements will be preserved.

元素中的新行/空格/IFS将被保留。

For copying associative arrays, Isaac's solutions work great.

对于复制关联数组,Isaac的解决方案非常有效。

#3


3  

The solutions given in the other answers won't work for associative arrays, or for arrays with non-contiguous indices. Here are is a more general solution:

其他答案中给出的解决方案不适用于关联数组或具有非连续索引的数组。下面是一个更普遍的解决方案:

declare -A arr=([this]=hello [\'that\']=world [theother]='and "goodbye"!')
temp=$(declare -p arr)
eval "${temp/arr=/newarr=}"

diff <(echo "$temp") <(declare -p newarr | sed 's/newarr=/arr=/')
# no output

And another:

另一个:

declare -A arr=([this]=hello [\'that\']=world [theother]='and "goodbye"!')
declare -A newarr
for idx in "${!arr[@]}"; do
    newarr[$idx]=${arr[$idx]}
done

diff <(echo "$temp") <(declare -p newarr | sed 's/newarr=/arr=/')
# no output

#4


1  

You can copy an array by inserting the elements of the first array into the copy by specifying the index:

您可以通过指定索引将第一个数组的元素插入到副本中来复制数组:

#!/bin/bash

array=( One Two Three Go! );
array_copy( );

let j=0;
for (( i=0; i<${#array[@]}; i++)
do
    if [[ $i -ne 1 ]]; then # change the test here to your 'isn't installed' test
        array_copy[$j]="${array[$i]}
        let i+=1;
    fi
done

for k in "${array_copy[@]}"; do
    echo $k
done

The output of this would be:

它的产出将是:

One
Three
Go!

A useful document on bash arrays is on TLDP.

关于bash阵列的有用文档在TLDP上。

#5


1  

Starting with Bash 4.3, you can do this

从Bash 4.3开始,您可以这样做。

$ alpha=(bravo charlie 'delta  3' '' foxtrot)

$ declare -n golf=alpha

$ echo "${golf[2]}"
delta  3

#6


-3  

I've discovered what was wrong.. My if isn't installed test is two for loops that remove excess characters from file names, and spits them out if they exist on a certain web server. What it wasn't doing was removing a trailing hyphen. So, when it tested it online for availability, they were parsed out. Because "file" exists, but "file-" doesn't.

我发现了问题所在。我的if not installed test是两个for循环,用于从文件名中删除多余的字符,如果它们存在于某个web服务器上,则将它们输出。它不做的是移除一个拖尾连字符。因此,当它在线测试可用性时,它们被解析了。因为"file"存在,而"file-"不存在。

#1


69  

a=(foo bar "foo 1" "bar two")  #create an array
b=("${a[@]}")                  #copy the array in another one 

for value in "${b[@]}" ; do    #print the new array 
echo "$value" 
done   

#2


8  

The simplest way to copy a non-associative array in bash is to:

在bash中复制非关联数组的最简单方法是:

arrayClone=("${oldArray[@]}")

arrayClone = " $ { oldArray[@]}”)

or to add elements to a preexistent array:

或向先前存在的数组添加元素:

someArray+=("${oldArray[@]}")

someArray + =(“$ { oldArray[@]}”)

Newlines/spaces/IFS in the elements will be preserved.

元素中的新行/空格/IFS将被保留。

For copying associative arrays, Isaac's solutions work great.

对于复制关联数组,Isaac的解决方案非常有效。

#3


3  

The solutions given in the other answers won't work for associative arrays, or for arrays with non-contiguous indices. Here are is a more general solution:

其他答案中给出的解决方案不适用于关联数组或具有非连续索引的数组。下面是一个更普遍的解决方案:

declare -A arr=([this]=hello [\'that\']=world [theother]='and "goodbye"!')
temp=$(declare -p arr)
eval "${temp/arr=/newarr=}"

diff <(echo "$temp") <(declare -p newarr | sed 's/newarr=/arr=/')
# no output

And another:

另一个:

declare -A arr=([this]=hello [\'that\']=world [theother]='and "goodbye"!')
declare -A newarr
for idx in "${!arr[@]}"; do
    newarr[$idx]=${arr[$idx]}
done

diff <(echo "$temp") <(declare -p newarr | sed 's/newarr=/arr=/')
# no output

#4


1  

You can copy an array by inserting the elements of the first array into the copy by specifying the index:

您可以通过指定索引将第一个数组的元素插入到副本中来复制数组:

#!/bin/bash

array=( One Two Three Go! );
array_copy( );

let j=0;
for (( i=0; i<${#array[@]}; i++)
do
    if [[ $i -ne 1 ]]; then # change the test here to your 'isn't installed' test
        array_copy[$j]="${array[$i]}
        let i+=1;
    fi
done

for k in "${array_copy[@]}"; do
    echo $k
done

The output of this would be:

它的产出将是:

One
Three
Go!

A useful document on bash arrays is on TLDP.

关于bash阵列的有用文档在TLDP上。

#5


1  

Starting with Bash 4.3, you can do this

从Bash 4.3开始,您可以这样做。

$ alpha=(bravo charlie 'delta  3' '' foxtrot)

$ declare -n golf=alpha

$ echo "${golf[2]}"
delta  3

#6


-3  

I've discovered what was wrong.. My if isn't installed test is two for loops that remove excess characters from file names, and spits them out if they exist on a certain web server. What it wasn't doing was removing a trailing hyphen. So, when it tested it online for availability, they were parsed out. Because "file" exists, but "file-" doesn't.

我发现了问题所在。我的if not installed test是两个for循环,用于从文件名中删除多余的字符,如果它们存在于某个web服务器上,则将它们输出。它不做的是移除一个拖尾连字符。因此,当它在线测试可用性时,它们被解析了。因为"file"存在,而"file-"不存在。