“声明”不会因缺少函数/命令而导致问题?

“声明”不会因缺少函数/命令而导致问题?

概括:如果bash我尝试将缺失函数的输出分配给先前的declare变量(即,不是{常量,只读})变量,我可以通过“正常”测试检测到失败。但是,如果我尝试在 ing 时将缺失函数的输出分配给变量declare(例如,使 var {constant,read-only}),则不仅分配不会在“正常”测试中失败,而且我也不能使用“正常”内置函数强制失败。我怎样才能使后一种情况失败?

细节:

我最近在一个更大的bash脚本中遇到了一个问题,我尝试将其分解为以下两个脚本。基本上,我有点用bash( snark > /dev/null) 做 TDD,所以除其他外

  • 我希望丢失的命令/功能能够快速失败
  • 我想防止重写常量

然而,bash似乎允许我declare在 var 时将缺失函数的输出分配给变量。例如,以下脚本(另存为/path/to/assign_at_declare.sh)...

#!/usr/bin/env bash

function foo() {
    return 0
}

# function bar() {}               # does not exist!

declare ret_val=''
declare -r MESSAGE_PREFIX="$(basename "${BASH_SOURCE}"):"
declare -r ERROR_PREFIX="${MESSAGE_PREFIX} ERROR:"

echo -e "\n${MESSAGE_PREFIX} a naïve 1st attempt:\n"

declare -ir FOO1_VAL="$(foo)"   # this should succeed, and does
ret_val="${?}"
if   [[ "${ret_val}" -ne 0  ]] ; then
    >&2 echo "${ERROR_PREFIX} foo returned '${ret_val}', exiting ..."
    exit 3
elif [[ -z "${FOO1_VAL}"  ]] ; then
    >&2 echo "${ERROR_PREFIX} foo returned null, exiting ..."
    exit 4
else
    echo "${MESSAGE_PREFIX} FOO1_VAL='${FOO1_VAL}'"
fi

declare -ir BAR1_VAL="$(bar)"   # this should fail ... but doesn't
ret_val="${?}"
if   [[ "${ret_val}" -ne 0  ]] ; then
    >&2 echo "${ERROR_PREFIX} bar returned '${ret_val}', exiting ..."
    exit 5
elif [[ -z "${BAR1_VAL}"  ]] ; then
    >&2 echo "${ERROR_PREFIX} bar returned null, exiting ..."
    exit 6
else
    echo "${MESSAGE_PREFIX} BAR1_VAL='${BAR1_VAL}'"
fi

echo -e "\n${MESSAGE_PREFIX} get tough using \`set\` builtins:\n"
# see https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
set -o errexit
set -o pipefail

declare -ir FOO2_VAL="$(foo)"   # this should succeed, and does
ret_val="${?}"
if   [[ "${ret_val}" -ne 0  ]] ; then
    >&2 echo "${ERROR_PREFIX} foo returned '${ret_val}', exiting ..."
    exit 3
elif [[ -z "${FOO2_VAL}"  ]] ; then
    >&2 echo "${ERROR_PREFIX} foo returned null, exiting ..."
    exit 4
else
    echo "${MESSAGE_PREFIX} FOO2_VAL='${FOO2_VAL}'"
fi

declare -ir BAR2_VAL="$(bar)"   # this should fail ... but doesn't
ret_val="${?}"
if   [[ "${ret_val}" -ne 0  ]] ; then
    >&2 echo "${ERROR_PREFIX} bar returned '${ret_val}', exiting ..."
    exit 5
elif [[ -z "${BAR2_VAL}"  ]] ; then
    >&2 echo "${ERROR_PREFIX} bar returned null, exiting ..."
    exit 6
else
    echo "${MESSAGE_PREFIX} BAR2_VAL='${BAR2_VAL}'"
fi

exit 0

...产生以下输出:

assign_at_declare.sh: a naïve 1st attempt:

assign_at_declare.sh: FOO1_VAL='0'
/path/to/assign_at_declare.sh: line 27: bar: command not found
assign_at_declare.sh: BAR1_VAL='0'

assign_at_declare.sh: get tough using `set` builtins:

assign_at_declare.sh: FOO2_VAL='0'
/path/to/assign_at_declare.sh: line 56: bar: command not found
assign_at_declare.sh: BAR2_VAL='0'

这看起来很奇怪,因为如果我尝试将缺失函数的输出分配给变量,我不会观察到这种行为 declareing var(即,如果 var 是不是{常量,只读})如以下脚本所示(另存为/path/to/assign_after_declare.sh)...

#!/usr/bin/env bash

function foo() {
    return 0
}

# function bar() {}           # does not exist!

declare ret_val=''
declare -i foo_val=0
declare -i bar_val=0
declare -r MESSAGE_PREFIX="$(basename "${BASH_SOURCE}"):"
declare -r ERROR_PREFIX="${MESSAGE_PREFIX} ERROR:"

echo -e "\n${MESSAGE_PREFIX} following works as expected\n"

foo_val="$(foo)"           # this should succeed, and does with/out `declare`
ret_val="${?}"
if   [[ "${ret_val}" -ne 0  ]] ; then
    >&2 echo "${ERROR_PREFIX} foo returned '${ret_val}', exiting ..."
    exit 3
elif [[ -z "${foo_val}"  ]] ; then
    >&2 echo "${ERROR_PREFIX} foo returned null, exiting ..."
    exit 4
else
    echo "${MESSAGE_PREFIX} foo_val='${foo_val}'"
fi

bar_val="$(bar)"           # this succeeds with `declare`, fails without
ret_val="${?}"
if   [[ "${ret_val}" -ne 0  ]] ; then
    >&2 echo "${ERROR_PREFIX} bar returned '${ret_val}', exiting ..."
    exit 5
elif [[ -z "${bar_val}"  ]] ; then
    >&2 echo "${ERROR_PREFIX} bar returned null, exiting ..."
    exit 6
else
    echo "${MESSAGE_PREFIX} bar_val='${bar_val}'"
fi

exit 0

...产生以下输出:

assign_after_declare.sh: following works as expected

assign_after_declare.sh: foo_val='0'
/path/to/assign_after_declare.sh: line 29: bar: command not found
assign_after_declare.sh: ERROR: bar returned '127', exiting ...

有没有办法bash在分配时强制快速失败期间A declare?如果是这样,我等待你的答复。

或者,是否bash按设计工作?如果是这样,请链接到参考文献;我尝试搜索这个问题,但我的选择器要么不正确,要么返回了太多不相关的响应,以至于毫无用处。

答案1

declare的返回状态是:

零,除非遇到无效选项,尝试使用 定义函数-f foo=bar,尝试将值分配给只读变量,尝试在不使用复合赋值语法的情况下将值分配给数组变量,其中一个名称不是有效的 shell 变量名称,尝试关闭只读变量的只读状态,尝试关闭数组变量的数组状态,或者尝试显示非现有函数与-f.

由于这些情况都不适用,所以正如您所说,Bash 正在按设计工作。

$(bar)在子 shell 中执行bar,该子 shell 退出时出现错误且没有标准输出。替换结果是一个空字符串,对于整型变量来说它被解释为零。declare然后返回 0,如文档所示


可以通过以下方式检测故障:

declare -ir x="$(bar || echo failed running bar >&3)"

预先适当设置 fd 3。这样的设置留给读者作为练习。

相关内容