unix shell,通过管道子项获取退出代码 [英] unix shell, getting exit code with piped child

查看:163
本文介绍了unix shell,通过管道子项获取退出代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我是在unix shell中执行此操作

Let's say I do this in a unix shell

$ some-script.sh | grep mytext

$ echo $?

这将给我grep

但是如何获取some-script.sh

编辑

假定管道操作是不可变的.即,我无法将其分解并分别运行两个命令

Assume that the pipe operation is immutable. ie, I can not break it apart and run the two commands seperately

推荐答案

有多种解决方案,这取决于您要确切执行的操作.

There are multiple solutions, it depends on what you want to do exactly.

最简单易懂的方法是将输出发送到文件,然后在保存退出代码后对其进行grep加密:

The easiest and understandable way would be to send the output to a file, then grep for it after saving the exit code:

tmpfile=$(mktemp)
./some-script.sh > $tmpfile
retval=$?
grep mytext $tmpfile
rm tmpfile

这篇关于unix shell,通过管道子项获取退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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