巴什复制从一个阵列到另一个 [英] Bash copy from one array to another

查看:107
本文介绍了巴什复制从一个阵列到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的数组。调用它,

  =决定(猫〜/ Depends.txt)

当我尝试解析列表,并用它复制到一个新的数组,

 为我在$ {取决于[@]}
   如果[$#我没有安装]。然后
      newDepends + =($ I)
   科幻
DONE

什么情况是,只依赖的第一个元素newDepends卷起。

 为我在$ {newDepends [@]}
   回声$我
DONE

^^这将输出一件事。所以我试图找出为什么我的for循环只有移动的第一要素。整个名单是原本就依赖,所以它不是这一点,但我所有的想法。


解决方案

  A =(富巴富1,巴二)#创建一个数组
B =($ {一个[@]})#copy阵列中另一个在值$ {B [@]};做#PRINT新数组
回声$值
DONE

I have an array of applications. Call it,

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.

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.

解决方案

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   

这篇关于巴什复制从一个阵列到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆