本文介绍了bash脚本,返回数组的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个bash脚本,我已经遇到了一个问题。所以我们可以说我得到了这个

I am making a bash script and I have encountered a problem. So let's say I got this

function create_some_array(){
  for i in 0 1 2 3 .. 10
  do
    a[i]=$i
  done
}

create_some_array
echo ${a[*]}

有没有什么办法可以使这项工作?我寻觅了很多,没有什么我找到的工作。
我想制作 A [] 全局变量应该工作,但我无法找到的东西,实际工作中我的code。有什么办法从函数的数组返回主程序?

Is there any way I can make this work? I have searched quite a lot and nothing I found worked.I think making the a[] a global variable should work but I can't find something that actually works in my code. Is there any way to return the array from the function to main program?

在此先感谢

推荐答案

描述这工作得很好。最可能的原因是不实际的code工作,是因为你碰巧在子shell中运行它:

This works fine as described. The most likely reason it doesn't work in your actual code is because you happen to run it in a subshell:

cat textfile | create_some_array
echo ${a[*]}

是行不通的,因为在管道运行的每一个元素在子shell,以及

would not work, because each element in a pipeline runs in a subshell, and

myvalue=$(create_some_array)
echo ${a[*]}

是行不通的,因为命令扩展在子shell中发生的。

would not work, since command expansion happens in a subshell.

这篇关于bash脚本,返回数组的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 09:06