忽略 zsh 和 bash 中的特定退出代码
Ignore specific exit code in zsh and bash
我正在寻找一种方法来忽略适用于 zsh 和 bash 的特定退出代码。
在我的示例中,我想忽略退出代码 134
。
我设法为 bash:
想出了一个工作示例
node fail.js || STATUS=$? && (if [ $STATUS == 134 ]; then true; else exit $STATUS; fi)
在此方面的任何改进也值得赞赏,但我的努力始于 zsh。
此命令在 zsh 中不起作用。我发现,zsh 中使用 ||
来拆分命令,但我还没有找到与我希望的工作方式等效的方法。
your_command || (exit "$(($? == 134 ? 0 : $?))")
这在 subshell 中使用 exit
将退出状态 134 更改为 0,否则如果 your_command
失败则传递该值。
$(( <em>expr</em> ))
用于 shell 算术展开。在此上下文中,二进制运算符 ==
和条件运算符 ?:
三元运算符是标准运算符。
我认为 ||
在 zsh 中不是问题。也许使用非标准的 ==
运算符和 [
而不是 [[
。 Zsh 在使用 [
或 test
.
时尝试符合 POSIX 标准
man zshbuiltins
说 test
/[
:
The command attempts to implement POSIX and its extensions where these are specified. Unfortunately there are intrinsic ambiguities in the syntax; in particular there is no distinction between test operators and strings that resemble them. The standard attempts to resolve these for small numbers of arguments (up to four); for five or more arguments compatibility cannot be relied on. Users are urged wherever possible to use the [[
test syntax which does not have these ambiguities.
如果您的意图是让脚本保持 运行 节点解释器不会失败,这可能看起来像:
node fail.js || { rc=$?; if [[ $rc != 134 ]]; then exit "$rc"; fi; }
我正在寻找一种方法来忽略适用于 zsh 和 bash 的特定退出代码。
在我的示例中,我想忽略退出代码 134
。
我设法为 bash:
node fail.js || STATUS=$? && (if [ $STATUS == 134 ]; then true; else exit $STATUS; fi)
在此方面的任何改进也值得赞赏,但我的努力始于 zsh。
此命令在 zsh 中不起作用。我发现,zsh 中使用 ||
来拆分命令,但我还没有找到与我希望的工作方式等效的方法。
your_command || (exit "$(($? == 134 ? 0 : $?))")
这在 subshell 中使用 exit
将退出状态 134 更改为 0,否则如果 your_command
失败则传递该值。
$(( <em>expr</em> ))
用于 shell 算术展开。在此上下文中,二进制运算符 ==
和条件运算符 ?:
三元运算符是标准运算符。
我认为 ||
在 zsh 中不是问题。也许使用非标准的 ==
运算符和 [
而不是 [[
。 Zsh 在使用 [
或 test
.
man zshbuiltins
说 test
/[
:
The command attempts to implement POSIX and its extensions where these are specified. Unfortunately there are intrinsic ambiguities in the syntax; in particular there is no distinction between test operators and strings that resemble them. The standard attempts to resolve these for small numbers of arguments (up to four); for five or more arguments compatibility cannot be relied on. Users are urged wherever possible to use the
[[
test syntax which does not have these ambiguities.
如果您的意图是让脚本保持 运行 节点解释器不会失败,这可能看起来像:
node fail.js || { rc=$?; if [[ $rc != 134 ]]; then exit "$rc"; fi; }