在 BASH / SHELL 中捕获输出和退出代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28902393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:29:40  来源:igfitidea点击:

Capturing output and exit codes in BASH / SHELL

bashshelloutputexit

提问by Flo

I am having trouble capturing output and exit codes inside a shell.

我在捕获 shell 中的输出和退出代码时遇到问题。

I need to compare exit codes from 2 scripts and if they don't match I want to echo the output of my 2 scripts.

我需要比较 2 个脚本的退出代码,如果它们不匹配,我想回显我的 2 个脚本的输出。

What I currently have:

我目前拥有的:

#!/bin/bash

resultA=$(./a.out 2>&1)
exitA=$?
resultB=$(./b.out 2>&1)
exitB=$?

Problem is a possible Segmentation Fault message is not captured, because it is directed to the error output of my current shell, but I need to capture everything including something like Segmentation Faults.

问题是未捕获可能的 Segmentation Fault 消息,因为它指向我当前 shell 的错误输出,但我需要捕获所有内容,包括诸如 Segmentation Faults 之类的内容。

What is kind of a workaround and not as detailed as the real message:

什么是一种解决方法,而不是像真实消息那样详细:

#!/bin/bash

resultA=$(./a.out 2>&1)
exitA=$?
resultB=$(./b.out 2>&1)
exitB=$?
if [ $exitA == 139 ]; then
    resultA=$resultA"Segmentation Fault"
fi

This makes the words segmentation fault at least appear in my result variables.

这使得单词分段错误至少出现在我的结果变量中。

回答by rici

It's possible to capture the segfault error message, but you really need to work at it.

可以捕获段错误错误消息,但您确实需要处理它。

Here's one way:

这是一种方法:

outputA=$(bash -c '(./a)' 2>&1)

Here we create an child shell (with bash -c) whose stderr is redirected to stdout, and then get that child to execute the program in an explicit subshell. Errors inside the subshell will be captured by the child bash, which will then generate an error message (which is not quite the same as the message produced by an interactive bash):

在这里,我们创建了一个子 shell (with bash -c),其 stderr 被重定向到 stdout,然后让该子 shell 在显式子 shell 中执行程序。子 shell 中的错误将被子 bash 捕获,然后会生成一条错误消息(与交互式 bash 产生的消息不太一样):

$ echo $outputA
bash: line 1: 11636 Segmentation fault (core dumped) ( ./a )

回答by Flo

Thanks to @rici this is the complete solution to my problem:

感谢@rici,这是我问题的完整解决方案:

#!/bin/bash

resultA=$(bash -c '(./a.out); exit $?' 2>&1)
exitA=$?
resultB=$(bash -c '(./b.out); exit $?' 2>&1)
exitB=$?

回答by Michael Jaros

Check out hek2mgl's answer for the reason for the missing message in your output.

查看 hek2mgl 的答案,了解输出中缺少消息的原因。

The Bash manpage provides a hint towards a solution:

Bash 联机帮助页提供了解决方案的提示:

When a command terminates on a fatal signal N, bash uses the value of 128+N as the exit status.

当命令在致命信号 N 上终止时,bash 使用 128+N 的值作为退出状态。

You could use this to handle the special case of a signal killing one of your child processes.

您可以使用它来处理信号杀死您的子进程之一的特殊情况。