防止bash打印“< PID>"已中止"消息 [英] Prevent bash from printing “<PID> Aborted” messages
问题描述
我有一个测试脚本,可以通过各种输入一次又一次地运行一个小应用程序:
I've got a test script that runs a small app over and over again with various inputs:
# test_script.sh
for input1 in $some_range; do
for input2 in $some_other_range; do
if ! ./my_app $input1 $input2 2>/dev/null; then
echo "ERROR: app failed with inputs: $input1 $input2"
fi
done
done
这一切都很好,除了失败时,我收到两条消息,我要的错误"消息,然后收到另一条消息(显然是bash发出的消息),提醒我我的应用程序已中止:
This is all well and good, except when it fails I get two messages, the 'ERROR' message I want, and then another (apparently from bash?) alerting me that my app was aborted:
test_script.sh: line 10: 641 Aborted ./my_app $input1 $input2
ERROR: app failed with inputs: XXX YYY
如何防止中止"消息?
还请注意:该应用程序可能无法在标准C库"assert"语句中失败.
Also note: The app is probably failing on a standard C library 'assert' statement.
推荐答案
我也遇到了这个问题.如果子进程返回状态代码134,表明子进程接收到 SIGABRT
,则 bash
本身似乎会单方面打印此消息.解决方案是在子shell中运行子进程,然后确保 subshell 在失败时返回不同的状态代码(仍为非零),并将其输出重定向到/dev/null
.例如:
I just ran into this too. It seems that bash
itself prints this unilaterally if a child process returns with status code 134, indicating the child recieved SIGABRT
. The solution is to run the child process in a subshell, then ensure the subshell returns a different (still non-zero) status code on failure and has its output redirected to /dev/null
. For example:
if ! ( ./myapp || false ) >/dev/null 2>&1; then
...
fi
这篇关于防止bash打印“< PID>"已中止"消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!