可能重复:
在bash shell脚本中标识接收到的信号名称
当使用类似的东西trap func_trap INT TERM EXIT
时:
func_trap () {
...some commands...
}
Run Code Online (Sandbox Code Playgroud)
功能块中有没有办法检测哪个陷阱调用了它?
就像是:
func_trap () {
if signal = INT; then
# do this
else
# do that
fi
}
Run Code Online (Sandbox Code Playgroud)
或者我是否需要为每个陷阱类型编写一个单独的函数来执行不同的操作?是否有一个bash变量保存最新收到的信号?
提前致谢!
我正在开发一个管理一些陷阱的脚本。一开始我只用这段代码管理 INT 和 SIGTSTP,它运行得很好:
#!/bin/bash
function capture_traps() {
echo -e "\nDoing something on exit"
exit 1
}
trap capture_traps INT
trap capture_traps SIGTSTP
read -p "Script do its stuff here and we use read for the example we pause for time to generate trap event"
exit 0
Run Code Online (Sandbox Code Playgroud)
然后我尝试添加我想要管理的新陷阱,即 SIGINT 和 SIGHUP。首先,我这样做了(这是有效的):
#!/bin/bash
function capture_traps() {
echo -e "\nDoing something on exit"
exit 1
}
trap capture_traps INT
trap capture_traps SIGTSTP
trap capture_traps SIGINT
trap capture_traps SIGHUP
read -p "Script do …
Run Code Online (Sandbox Code Playgroud)