将数组从一个Bash脚本传递到另一个

时间:2022-04-20 01:47:12

I am new to writing Shell Scripts and am having some difficulties.

我是编写Shell Scripts的新手,我遇到了一些困难。

What I Want To Achieve

我想要实现的目标

I have an array of strings in scriptOne.sh that I want to pass to scriptTwo.sh

我在scriptOne.sh中有一个字符串数组,我想传递给scriptTwo.sh

What I Have Done So Far

我到目前为止做了什么

I can execute the second script from inside the first using ./scriptTwo.sh and I have passed string variables from one to the other using ./scriptTwo.sh $variableOne.

我可以使用./scriptTwo.sh从第一个脚本中执行第二个脚本,并使用./scriptTwo.sh $ variableOne将字符串变量从一个传递到另一个。

The issues is when I try to pass an array variable it doesn't get passed. I have managed to get it to pass the first entry of the array using ./scriptTwo.sh "${array[@]}" however this is only one of the entries and I need all of them.

问题是当我尝试传递一个数组变量时,它没有被传递。我设法让它使用./scriptTwo.sh“$ {array [@]}”传递数组的第一个条目,但这只是其中一个条目,我需要所有条目。

Thanks in advance for your help

在此先感谢您的帮助

1 个解决方案

#1


7  

Your way of passing the array is correct

你传递数组的方式是正确的

./scriptTwo.sh "${array[@]}"

The problem is probably in the way how you receive it. In scriptTwo.sh, use

问题可能在于您收到它的方式。在scriptTwo.sh中,使用

array=("$@")

#1


7  

Your way of passing the array is correct

你传递数组的方式是正确的

./scriptTwo.sh "${array[@]}"

The problem is probably in the way how you receive it. In scriptTwo.sh, use

问题可能在于您收到它的方式。在scriptTwo.sh中,使用

array=("$@")