重定向./a.out不会捕获分段错误

cha*_*nda 8 c c++ linux bash shell

我运行命令

./a.out < in &> output.txt
我希望错误也被放入output.txt.
exit命令的状态为139,在终端上的输出为:

Segmentation fault (core dumped)

并且文件output.txt是空的.

P.P*_*.P. 17

该消息Segmentation fault (core dumped)不是从你的程序来.

它是由shell产生的信号产生的.它不是程序的stderrstdout的一部分.

所以shell的消息可以捕获为:

{ ./a.out; } 2> out_err 
Run Code Online (Sandbox Code Playgroud)


Rob*_*ale 5

如果您想要来自的错误消息a.out和字符串

Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)

要附加到,那么您还output.txt必须重定向 shell 。stderr例如,

exec 2>> output.txt && ./a.out < in 2>&1 >> output.txt &
Run Code Online (Sandbox Code Playgroud)

这是因为段错误消息来自 shell 本身。