将数组从一个 Bash 脚本传递到另一个
我是编写 Shell 脚本的新手,遇到了一些困难.
I am new to writing Shell Scripts and am having some difficulties.
我想要达到的目标
我在 scriptOne.sh
中有一个字符串数组,我想将其传递给 scriptTwo.sh
I have an array of strings in scriptOne.sh
that I want to pass to scriptTwo.sh
到目前为止我做了什么
我可以使用 ./scriptTwo.sh
从第一个脚本内部执行第二个脚本,并且我使用 ./scriptTwo.sh $variableOne
将字符串变量从一个传递到另一个脚本代码>.
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 "${array[@]}"
传递数组的第一个条目,但这只是其中一个条目,我需要所有这些条目.
The issues are 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.
预先感谢您的帮助
你传递数组的方式是正确的
Your way of passing the array is correct
./scriptTwo.sh "${array[@]}"
问题可能在于您收到它的方式.在 scriptTwo.sh
中,使用
The problem is probably in the way how you receive it. In scriptTwo.sh
, use
array=("$@")