我想捕获在最后一个位置之前在管道中某处发生的命令的退出状态。例如,如果管道类似于
command_1 ... | command_2 ... | command_3 ... | ... | command_n
Run Code Online (Sandbox Code Playgroud)
...我想知道如何捕获command_1
, or of command_2
, or ofcommand_3
等的退出状态command_n
(当然,捕获 的退出状态是微不足道的。)
此外,万一重要,这条管道发生在 zsh shell 函数内。
我试图command_1
用类似的东西来捕捉退出状态
function_with_pipeline () {
local command_1_status=-999999 # sentinel value
{ command_1 ...; command_1_status=$? } | command_2 ... | ... | command_n
...
}
Run Code Online (Sandbox Code Playgroud)
...但运行管道后,command_1_status
变量的值仍然是哨兵值。
FWIW,这是一个工作示例,其中管道只有两个命令:
foo ... | grep ...
Run Code Online (Sandbox Code Playgroud)
foo
是为了这个例子而定义的函数,像这样:
foo () {
(( $1 & 1 )) && echo "a non-neglible message"
(( $1 & 2 )) && echo "a negligible message"
(( $1 & 4 )) && echo "error message" >&2
return $(( ( $1 & 4 ) >> 2 ))
}
Run Code Online (Sandbox Code Playgroud)
目标是foo
在管道中捕获调用的退出状态。
该函数function_with_pipeline
实现了我上面描述的(最终无效)策略来做到这一点:
function_with_pipeline () {
local foo_status=-999999 # sentinel value
{ foo $1; foo_status=$? } | grep -v "a negligible message"
printf '%d\ndesired: %d; actual: %d\n\n' $1 $(( ( $1 & 4 ) >> 2 )) $foo_status
}
Run Code Online (Sandbox Code Playgroud)
下面的循环练习这个function_with_pipeline
功能。输出显示局部变量的值foo_status
最终与它开始时没有什么不同。
for i in $(seq 0 7)
do
function_with_pipeline $i
done
# 0
# desired: 0; actual: -999999
#
# a non-neglible message
# 1
# desired: 0; actual: -999999
#
# 2
# desired: 0; actual: -999999
#
# a non-neglible message
# 3
# desired: 0; actual: -999999
#
# error message
# 4
# desired: 1; actual: -999999
#
# error message
# a non-neglible message
# 5
# desired: 1; actual: -999999
#
# error message
# 6
# desired: 1; actual: -999999
#
# error message
# a non-neglible message
# 7
# desired: 1; actual: -999999
#
Run Code Online (Sandbox Code Playgroud)
我得到同样的结果,如果我省略了local
在定义声明foo_status
。
有特殊的阵列pipestatus
对于在zsh
,所以尝试
command_1 ... | command_2 ... | command_3
Run Code Online (Sandbox Code Playgroud)
和
echo $pipestatus[1] $pipestatus[2] $pipestatus[3]
Run Code Online (Sandbox Code Playgroud)
并且您的方法不起作用的原因是因为每个管道都在单独的子外壳中运行,具有自己的变量,一旦退出子外壳,这些变量就会被销毁。
仅供参考,它PIPESTATUS
在bash
.
归档时间: |
|
查看次数: |
1219 次 |
最近记录: |