Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Bash脚本-函数退出时全局变量变为空 问询处_Arrays_Bash_Shell_Command Line - Fatal编程技术网

Arrays Bash脚本-函数退出时全局变量变为空 问询处

Arrays Bash脚本-函数退出时全局变量变为空 问询处,arrays,bash,shell,command-line,Arrays,Bash,Shell,Command Line,我有一个问题,我想从bash脚本函数返回一个值(数组或字符串) 我已经知道: 名为outputtext=$(functionName)的bash脚本在子shell中运行 bash函数的作用与“普通”编程语言不同,它只返回文本输出 bash函数只能返回错误代码作为状态,并且可以通过$? 测试脚本 我现在创建了一个测试脚本来演示我想做什么,以及为什么我不能直接使用输出 #!/bin/bash declare GLOBAL_VAR function callThis(){

我有一个问题,我想从bash脚本函数返回一个值(数组或字符串)

我已经知道:

  • 名为
    outputtext=$(functionName)
    的bash脚本在子shell中运行
  • bash函数的作用与“普通”编程语言不同,它只返回文本输出
  • bash函数只能返回错误代码作为状态,并且可以通过
    $?
测试脚本 我现在创建了一个测试脚本来演示我想做什么,以及为什么我不能直接使用输出

   #!/bin/bash
   declare GLOBAL_VAR
   function callThis(){
      local output="==========================================\n"
      output="$output [START callThis()]\n"
      # For demonstration only - error 5 might be a built-in bash error code, but if caller
      # expects 0/5 they can be handled
      local statusCode=5
      # This variable is empty as soon as it exits the function
      GLOBAL_VAR="Some array value OR text output into GLOBAL_VAR"
      # Because the script has alot of text (echo'd/printf) output, and the function
      # calculates a result (array), I would like to have it as a RESULT
      # for another function.
      output="$output This is some output that will be sent to caller.\n"
      output="$output Test to see if GLOBAL_VAR is assigned:\n"
      output="$output '$GLOBAL_VAR'\n"
      output="$output [END callThis()]\n"
      output="$output ==========================================\n"
      echo -e $output
      return $((statusCode))
   }

   function startHere(){
      OUTPUT=$(callThis)
      STATUS_RESULT=$?
      echo -e "[Text output : $OUTPUT\n]"
      echo -e "[Status Code : $STATUS_RESULT\n]"
      echo -e "[Global VAR in startHere():\n'$GLOBAL_VAR']"
      if [ "$STATUS_RESULT" -eq 5 ]; then
         echo "Success! Status code $STATUS_RESULT found!"
      fi
   }

   echo -e $(startHere)
   echo "Global VAR is now (outside functions): '$GLOBAL_VAR'"
以上结果 我知道echo-e并没有给出这个函数的确切输出,除了这个主题

上面的脚本显示了我想做的大部分事情:我有相当长的函数,有计算结果数组,并处理退出代码

问题: 由于它是在调用
$(startHere)
时创建的子shell,甚至是在
startHere()
中创建的子shell,
全局变量
一旦退出
callThis()
就为空

我不知道如何继续,因为我需要另一个函数的值。我怎样才能解决这个问题

解决方案 有关我提供的示例,请参见@mjrezaee的解决方案


事实证明,我并不总是需要对所有事情都使用sub-shell
$()
,大多数时候直接调用函数,输出、全局变量甚至错误代码(返回代码)都是可用的。(我很少使用bash脚本)。根据建议,我修改了400多行脚本,能够轻松使用全局变量。

正如我在评论中所说,您可以使用简单的函数调用,对测试代码的简单更改如下:

   #!/bin/bash
   declare GLOBAL_VAR
   function callThis(){
      local output="==========================================\n"
      output="$output [START callThis()]\n"
      # For demonstration only - error 5 might be a built-in bash error code, but if caller
      # expects 0/5 they can be handled
      local statusCode=5
      # This variable is empty as soon as it exits the function
      GLOBAL_VAR="Some array value OR text output into GLOBAL_VAR"
      # Because the script has alot of text (echo'd/printf) output, and the function
      # calculates a result (array), I would like to have it as a RESULT
      # for another function.
      output="$output This is some output that will be sent to caller.\n"
      output="$output Test to see if GLOBAL_VAR is assigned:\n"
      output="$output '$GLOBAL_VAR'\n"
      output="$output [END callThis()]\n"
      output="$output ==========================================\n"
      echo -e $output
      return $((statusCode))
   }

   function startHere(){
      # OUTPUT=$(callThis)
      callThis
      STATUS_RESULT=$?
      # echo -e "[Text output : $OUTPUT\n]"
      echo -e "[Status Code : $STATUS_RESULT\n]"
      echo -e "[Global VAR in startHere(): '$GLOBAL_VAR']"
      if [ "$STATUS_RESULT" -eq 5 ]; then
         echo "Success! Status code $STATUS_RESULT found!"
      fi
   }
   # echo -e $(startHere)
   startHere
   echo "Global VAR is now (outside functions): '$GLOBAL_VAR'"
输出:

==========================================
 [START callThis()]
 This is some output that will be sent to caller.
 Test to see if GLOBAL_VAR is assigned:
 'Some array value OR text output into GLOBAL_VAR'
 [END callThis()]
 ==========================================

[Status Code : 5
]
[Global VAR in startHere(): 'Some array value OR text output into GLOBAL_VAR']
Success! Status code 5 found!
Global VAR is now (outside functions): 'Some array value OR text output into GLOBAL_VAR'

还可以帮助解释使用子shell的其他方法

这将很有帮助,我猜
echo
将在函数中工作,而不是通过子shell将其写入另一个变量。结果代码可以被
$?
捕获,因为你在代码中使用了你已经有东西要读1个小时了,但是不管怎样,你不必(或者实际上不应该)在问题被回答时更新它——事实上,有一个被接受的答案已经涵盖了这一点。如果我知道怎么做,我会把它作为答案发布,但我也在学习;)我希望能有类似于这里的字符串
>>
来将fd写入变量(在任何地方都没有发现)是的,谢谢,我已经从您最初的评论中了解到了这一点。我现在尝试更新我的长脚本,以使用这种调用函数的方法,而不是
$()子shell
==========================================
 [START callThis()]
 This is some output that will be sent to caller.
 Test to see if GLOBAL_VAR is assigned:
 'Some array value OR text output into GLOBAL_VAR'
 [END callThis()]
 ==========================================

[Status Code : 5
]
[Global VAR in startHere(): 'Some array value OR text output into GLOBAL_VAR']
Success! Status code 5 found!
Global VAR is now (outside functions): 'Some array value OR text output into GLOBAL_VAR'