unix shell,使用管道子进程获取退出代码

发布于 2024-09-02 12:36:45 字数 255 浏览 5 评论 0原文

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

$ some-script.sh | grep mytext

$ echo $?

这将为我提供 grep 的退出代码

,但是如何获得 some-script.sh 的退出代码

编辑

假设管道操作是不可变的。即,我无法将其分开并单独运行这两个命令

Let's say I do this in a unix shell

$ some-script.sh | grep mytext

$ echo $?

this will give me the exit code of grep

but how can I get the exit code of some-script.sh

EDIT

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

揽清风入怀 2024-09-09 12:36:45

有多种解决方案,这取决于您具体想要做什么。

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

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

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

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
夏日浅笑〃 2024-09-09 12:36:45

comp.unix.shell 常见问题解答 (#13) 解释了如何在 Bourne shell 中使用管道来帮助完成您想要的任务:

 您需要使用一个技巧将退出代码传递给主程序
   壳。您可以使用管道(2) 来完成此操作。而不是跑步
   “cmd1”,您运行“cmd1; echo $?”并确保 $?让路
   到外壳。

   执行3>&1
   评估`
     # 现在,在 `...` 内部,fd4 进入管道
     # 读取其另一端并传递给 eval;
     # fd1是保存的正常标准输出
     # exec 3>&1 之前的行
     执行 4>&1>&3 3>&- 
     {
       cmd1 4>&-;回声“ec1=$?;” >&4
     } | {
       cmd2 4>&-;回声“ec2=$?;” >&4
     } |指令3
     回声“ec3=$?;” >&4

A trick from the comp.unix.shell FAQ (#13) explains how using the pipeline in the Bourne shell should help accomplish what you want:

   You need to use a trick to pass the exit codes to the main
   shell.  You can do it using a pipe(2). Instead of running
   "cmd1", you run "cmd1; echo $?" and make sure $? makes it way
   to the shell.

   exec 3>&1
   eval `
     # now, inside the `...`, fd4 goes to the pipe
     # whose other end is read and passed to eval;
     # fd1 is the normal standard output preserved
     # the line before with exec 3>&1
     exec 4>&1 >&3 3>&- 
     {
       cmd1 4>&-; echo "ec1=$?;" >&4
     } | {
       cmd2 4>&-; echo "ec2=$?;" >&4
     } | cmd3
     echo "ec3=$?;" >&4
我要还你自由 2024-09-09 12:36:45

如果您使用的是 bash:

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

If you're using bash:

PIPESTATUS
    An array variable (see Arrays) containing a list of exit status values from the processes in the most-recently-executed foreground pipeline (which may contain only a single command). 
驱逐舰岛风号 2024-09-09 12:36:45

有一个名为 mispipe 的实用程序,它是 moreutils 包的一部分。

它正是这样做的:mispipe some-script.sh 'grep mytext'

There is a utility named mispipe which is part of the moreutils package.

It does exactly that: mispipe some-script.sh 'grep mytext'

儭儭莪哋寶赑 2024-09-09 12:36:45

第一种方法,将退出状态临时保存在某个文件中。这导致您必须使用大括号创建子 shell:

(your_script.sh.pl.others; echo $? >/tmp/myerr)|\ #subshell with exitcode saving
grep sh #next piped commands
exitcode=$(cat /tmp/myerr) #restore saved exitcode
echo $exitcode  #and print them

上面 Randy 提出的另一种方法,更简单的代码实现:

some-script.sh | grep mytext
echo ${PIPESTATUS[0]} #print exitcode for first commands. tables are indexted from 0

它的全部。两者都可以在 bash 下工作(我知道,bashizm)。祝你好运 :)
这两种方法都不将临时管道保存到物理文件,仅保存退出代码。

First approach, temporarly save exit status in some file. This cause you must create subshell using braces:

(your_script.sh.pl.others; echo $? >/tmp/myerr)|\ #subshell with exitcode saving
grep sh #next piped commands
exitcode=$(cat /tmp/myerr) #restore saved exitcode
echo $exitcode  #and print them

another approach presented by Randy above, simplier code implementation:

some-script.sh | grep mytext
echo ${PIPESTATUS[0]} #print exitcode for first commands. tables are indexted from 0

its all. both works under bash (i know, bashizm). good luck :)
both approaches does not save temporarly pipe to physical file, only exit code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文