加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash脚本:如何在管道中保存第一个命令的返回值?

发布时间:2020-12-16 01:36:21 所属栏目:安全 来源:网络整理
导读:Bash:我想运行一个命令并通过一些过滤器来管理结果,但如果命令失败,我想返回命令的错误值,而不是过滤器的无聊返回值: 例如.: if !(cool_command | output_filter); then handle_the_error; fi 要么: set -ecool_command | output_filter 在这两种情况下,
Bash:我想运行一个命令并通过一些过滤器来管理结果,但如果命令失败,我想返回命令的错误值,而不是过滤器的无聊返回值:

例如.:

if !(cool_command | output_filter); then handle_the_error; fi

要么:

set -e
cool_command | output_filter

在这两种情况下,这是我关心的cool_command的返回值 – 第一种情况下的’if’条件,或者在第二种情况下退出脚本.

有没有一些干净的成语这样做?

使用PIPESTATUS内置变量.

从男人bash:

PIPESTATUS

An array variable (see Arrays below) containing a list of exit status values from the processes in the most-recently-executed foreground pipeline (which may contain only a single command).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读