gpt4 book ai didi

arrays - Bash 脚本 - 函数退出时全局变量变为空

转载 作者:行者123 更新时间:2023-12-04 09:22:38 24 4
gpt4 key购买 nike

信息
我有一个问题,我想从 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 并没有为这个函数提供准确的输出,除了这个主题
       [Text output :
    ==========================================
    [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(): '']
    Success! Status code 5 found!
    Global VAR is now (outside functions): ''
    上面的脚本显示了我想要做的大部分事情:我有很长的函数,并计算了结果数组,并处理了退出代码。

    由于是调用 $(startHere)时创建的子shell ,甚至在 startHere() 内, GLOBAL_VAR一空就 callThis()退出。
    我不知道如何继续,因为我需要另一个函数的值。我该如何解决这个问题?
    解决方案
    有关我提供的示例,请参阅 @mjrezaee 的解决方案。
    事实证明,我并不总是需要使用子 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'
    还有 this可以帮助解释使用子 shell 的其他方法

    关于arrays - Bash 脚本 - 函数退出时全局变量变为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63073006/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com