相关疑难解决方法(0)

是否有可能在bash中检测*哪个*陷阱信号?

可能重复:
在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变量保存最新收到的信号?

提前致谢!

bash bash-trap

23
推荐指数
2
解决办法
9975
查看次数

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)

bash bash-trap

4
推荐指数
1
解决办法
1299
查看次数

标签 统计

bash ×2

bash-trap ×2