摘要:如果bash
我尝试将缺失函数的输出分配给先前的declare
d(即,不是 {constant,read-only})变量,我可以通过“正常”测试检测到失败。但是,如果我在对它进行操作时尝试将缺失函数的输出分配给一个变量declare
(例如,使 var {constant, read-only}),不仅分配不会因“正常”测试而失败,而且我不能使用“正常”内置函数强制失败。我怎样才能使后一种情况失败?
细节:
我最近在一个更大的bash
脚本中遇到了一个问题,我试图将其提炼成以下 2 个脚本。基本上,我有点像用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'
这看起来很奇怪,因为如果我在输入 var后 declare
尝试将缺失函数的输出分配给变量(即,如果 var不是{constant, read-only}) ,我没有观察到这种行为,如以下脚本所示(另存为/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 ...
在 a 期间分配时有没有办法强制bash
快速失败?如果是这样,我等待您的答复。declare
或者,是否bash
按设计工作?如果是这样,请链接到参考资料;我试着搜索这个问题,但我的选择器要么不正确,要么返回了太多不相关的响应,以至于毫无用处。
declare
的返回状态是:由于这些情况都不适用,正如您所说,Bash 正在按设计工作。
$(bar)
在子 shell 中执行bar
,该子 shell 退出时出现错误并且没有标准输出。替换产生一个空字符串,对于整数变量,它被解释为零。declare
然后返回 0,如文档所述。您可以通过以下方式检测故障:
预先适当设置 fd 3。这样的设置留给读者作为练习。