在bash中杀死后如何抑制终止消息? [英] How to suppress Terminated message after killing in bash?

查看:20
本文介绍了在bash中杀死后如何抑制终止消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何抑制在你杀死一个人之后出现的Terminated消息在 bash 脚本中处理?

How can you suppress the Terminated message that comes up after you kill a process in a bash script?

我试过 set +bm,但是不行.

我知道另一个解决方案是调用 exec 2>/dev/null,但是可靠的?如何将其重置,以便我可以继续查看 stderr?

I know another solution involves calling exec 2> /dev/null, but is that reliable? How do I reset it back so that I can continue to see stderr?

推荐答案

简短的回答是你不能.Bash 总是打印前台作业的状态.监控标志仅适用于后台作业,仅适用于交互式 shell,不适用于脚本.

The short answer is that you can't. Bash always prints the status of foreground jobs. The monitoring flag only applies for background jobs, and only for interactive shells, not scripts.

参见jobs.c中的notify_of_job_status()

see notify_of_job_status() in jobs.c.

正如您所说,您可以重定向,因此标准错误指向/dev/null 但随后您会错过任何其他错误消息.您可以通过在运行脚本的子shell 中进行重定向来使其临时化.这留下了原始环境.

As you say, you can redirect so standard error is pointing to /dev/null but then you miss any other error messages. You can make it temporary by doing the redirection in a subshell which runs the script. This leaves the original environment alone.

(script 2> /dev/null)

这将丢失所有错误消息,但仅来自该脚本,而不来自该 shell 中运行的任何其他内容.

which will lose all error messages, but just from that script, not from anything else run in that shell.

您可以通过将新的文件描述符重定向到那里来保存和恢复标准错误:

You can save and restore standard error, by redirecting a new filedescriptor to point there:

exec 3>&2          # 3 is now a copy of 2
exec 2> /dev/null  # 2 now points to /dev/null
script             # run script with redirected stderr
exec 2>&3          # restore stderr to saved
exec 3>&-          # close saved version

但我不推荐这个——第一个的唯一好处是它节省了一个子 shell 调用,同时更复杂,甚至可能改变脚本的行为,如果脚本改变了文件描述符.

But I wouldn't recommend this -- the only upside from the first one is that it saves a sub-shell invocation, while being more complicated and, possibly even altering the behavior of the script, if the script alters file descriptors.

要获得更合适的答案,请检查 Mark Edgar

这篇关于在bash中杀死后如何抑制终止消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆