是否可以在 bash 中检测 *which* 陷阱信号?

发布于 2024-08-20 00:19:45 字数 607 浏览 5 评论 0原文

可能的重复:
在 bash shell 脚本中识别接收到的信号名称

当使用类似的东西时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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多情癖 2024-08-27 00:19:45

您可以实现自己的陷阱函数,自动将信号传递给函数:

trap_with_arg() {
    func="$1" ; shift
    for sig ; do
        trap "$func $sig" "$sig"
    done
}

$ trap_with_arg func_trap INT TERM EXIT

func_trap 的第一个参数将是信号的名称。

You can implement your own trap function that automatically passes the signal to the function:

trap_with_arg() {
    func="$1" ; shift
    for sig ; do
        trap "$func $sig" "$sig"
    done
}

$ trap_with_arg func_trap INT TERM EXIT

The first argument to func_trap will be the name of the signal.

何其悲哀 2024-08-27 00:19:45

没有任何文档提示任何参数或变量保存被捕获的信号,因此您必须为每个要表现不同的陷阱编写一个函数/陷阱语句。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文