是否可以在 bash 中检测 *which* 陷阱信号?
当使用类似的东西时trap func_trap INT TERM EXIT
with:
func_trap () {
...some commands...
}
功能块中是否有办法检测哪个陷阱调用了它?
比如:
func_trap () {
if signal = INT; then
# do this
else
# do that
fi
}
或者我是否需要为每个陷阱类型编写一个单独的函数来执行不同的操作? 是否有一个 bash 变量保存最新接收到的信号?
提前致谢!
Possible Duplicate:
Identifying received signal name in bash shell script
When using something like trap func_trap INT TERM EXIT
with:
func_trap () {
...some commands...
}
Is there a way in the function block to detect which trap has called it?
Something like:
func_trap () {
if signal = INT; then
# do this
else
# do that
fi
}
Or do I need to write a separate function for each trap type that does something different?
Is there a bash variable that holds the latest received signal?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以实现自己的陷阱函数,自动将信号传递给函数:
func_trap 的第一个参数将是信号的名称。
You can implement your own trap function that automatically passes the signal to the function:
The first argument to func_trap will be the name of the signal.
没有任何文档提示任何参数或变量保存被捕获的信号,因此您必须为每个要表现不同的陷阱编写一个函数/陷阱语句。
No documentation hints of any argument or variable holding the signal that was trapped, so you'll have to write a function/trap statement for each trap you want to behave differently.