命令替换后管状态 [英] Pipe status after command substitution

查看:73
本文介绍了命令替换后管状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寄一系列命令给一个变量的结果是:

I'd like to send the result of a series of commands to a variable:

variable=$(a | few | commands)

不过,命令替换重置 PIPESTATUS ,所以我不能检查是哪里出了事后错了。一个解决办法是使用 mktemp的并暂时把结果有:

However, the command substitution resets PIPESTATUS, so I can't inspect where it went wrong after the fact. One solution would be to use mktemp and put the result there temporarily:

variable_file=$(mktemp) || exit 1
a | few | commands > $variable_file
exit_codes="${PIPESTATUS[*]}"
variable=$(<$variable_file)

有没有更好的解决方案?

Is there a more elegant solution?

推荐答案

还挺哈克,但我认为你可以掰过来这样。

Kinda hacky but I think you could fudge it like this.

variable=$(a | few | commands; echo ": ${PIPESTATUS[*]}")
PIPESTATUS=(${variable##*: })
variable=${variable%:*}
variable=${variable%$'\n'}

这篇关于命令替换后管状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆