在bash脚本标准错误的临时重定向 [英] Temporary redirection of stderr in a bash script
问题描述
我有一个用于启动其他程序的简单脚本。这其他程序有时可能会产生一个 SIGSEGV
,扰乱我的输出。因此,我增加了几个这是应该暂时重定向标准错误
到的/ dev / null的
使得线 SIGSEGV
被忽略。以下是我的code的草稿:
I have a simple script which is used to start another program. This other program may sometimes yield a SIGSEGV
, which disrupts my output. I have therefore added a couple of lines which is supposed to temporarily redirect the stderr
to /dev/null
such that the SIGSEGV
is ignored. The following is a draft of my code:
exec 2> /dev/null
progname >& ./tmp/run.txt && run_status='OK'
exec 2>1
问题是,最后一行没有做我想做的。第一行显然工程,并重定向标准错误
。最后一行应该返回标准错误
回其原来的位置(我只应该是一样的标准输出
)。
The problem is that the last line does not do what I want. The first line obviously works, and redirects the stderr
. The last line is supposed to return the stderr
back to where it was before (which I have only supposed is the same as stdout
).
任何帮助将appriciated!
Any help would be appriciated!
推荐答案
另外一个选择是:
exec 3> /dev/stderr 2> /dev/null
progname >& ./tmp/run.txt && run_status='OK'
exec 2>&3
甚至
exec 3>&2 2> /dev/null
progname >& ./tmp/run.txt && run_status='OK'
exec 2>&3
这样的脚本preserves输出和错误的脚本(分离即脚本stdout和stderr可以单独进行重定向。
That way the script preserves the separation of stdout and stderr for the script (ie. the scripts stdout and stderr can be redirected separately.
这篇关于在bash脚本标准错误的临时重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!