bash 检查标准输出或标准错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2325152/
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-17 21:43:23  来源:igfitidea点击:

Check for stdout or stderr

bashshellsegmentation-faultstdoutstderr

提问by Kiran

One of the binaries which I am using in my shell script is causing a segmentation fault (RETURN VALUE: 139)

我在 shell 脚本中使用的二进制文件之一导致分段错误(返回值:139)

And even though, I am redirecting both stdout and stderr to a logfile, the Segmentation Fault error messages is displayed in the terminal when I am running the shell script.

即使我将 stdout 和 stderr 都重定向到日志文件,当我运行 shell 脚本时,终端中会显示分段错误错误消息。

Is it possible to redirect this message from Segfault to a logfile ??

是否可以将此消息从 Segfault 重定向到日志文件?

采纳答案by Kiran

Well, I am answering my own question.. :) I found the answer here How can I suppress the output due to a SIGSEGV or a SIGFPE in a Fortran 90 program?

好吧,我正在回答我自己的问题.. :) 我在这里找到了答案 如何抑制 Fortran 90 程序中的 SIGSEGV 或 SIGFPE 的输出?

The trick is to add

诀窍是添加

`exec 2> filename`  

in the shell script.

在 shell 脚本中。

This will redirect all the messages from the shell to a log file

这会将所有消息从 shell 重定向到日志文件

回答by Kjetil Joergensen

The Segmentation Fault message you see is printed by the shell that is running your program. This behavior varies from shell to shell, so a few things you could try (if you insist on getting the segmentation fault message into your logs from shell-redirects).

您看到的 Segmentation Fault 消息是由运行您的程序的 shell 打印的。这种行为因外壳而异,因此您可以尝试一些方法(如果您坚持将分段错误消息从外壳重定向中获取到您的日志中)。

# Have sh invoke your program, and redirect output from both sh and your program into logfile
sh -c "program arguments more arguments" >logfile 2>&1
# Force bash to not just exec your program (/bin/true part), and redirect output
# from both bash and your program into logfile
bash -c "/bin/true; program arguments more arguments" >logfile 2>&1

回答by ghostdog74

try

尝试

./program &> logfile

there are various exampls on I/O redirection here, have a look

有各种exampls在I / O重定向这里,看看

You can take a look at this discussionas well

你可以看看这个讨论以及