脚本1.sh:
#!/bin/bash
./script2.sh
echo after-script
脚本2.sh:
#!/bin/bash
function handler {
exit 130
}
trap handler SIGINT
while true; do true; done
When I start script1.sh from a terminal, and then use Ctrl+C to send SIGINT to its process group, the signal is trapped by script2.sh and when script2.sh terminates, script1.sh prints "after-script". However, I would have expected script1.sh to immediately terminate after the line that invokes script2.sh. Why is this not the case in this example?
补充说明(编辑):
As script1.sh and script2.sh are in the same process group, SIGINT gets sent to both scripts when Ctrl+C is pressed on the command line. That's why I wouldn't expect script1.sh to continue when script2.sh exits.
当 script2.sh 中的“trap handler SIGINT”行被注释掉时,script1.sh 会在 script2.sh 存在后立即退出。我想知道为什么它的行为不同,因为 script2.sh 产生相同的退出代码 (130)。
新答案:
这个问题远比我最初想象的有趣。答案基本上在这里给出:
当发送到包含子项的 Perl 脚本时,SIGINT (^C) 会发生什么情况? https://stackoverflow.com/questions/4717118/what-happens-to-a-sigint-c-when-sent-to-a-perl-script-containing-children
这是相关的花絮。我知道您没有使用 Perl,但我认为 Bash 使用的是 C 的约定。
Perl 的内置系统函数的工作方式与 C 系统类似(3)
就信号而言,来自标准 C 库的函数。
如果您使用 Perl 版本的 system() 或管道打开或反引号,
然后是父级——一个调用系统而不是被调用的系统
它 - 当孩子们在的时候会忽略任何 SIGINT 和 SIGQUIT
跑步。
这个解释 http://www.cons.org/cracauer/sigint.html这是我见过的关于可以做出的各种选择的最好的。它还说 Bash 采用 WCE 方法。也就是说,当父进程收到 SIGINT 时,它会等待子进程返回。如果该进程从 SIGINT 处理退出,它也会以 SIGINT 退出。如果子进程以任何其他方式退出,它将忽略 SIGINT。
还有一种方法可以让调用 shell 判断被调用的是否是
程序在 SIGINT 时退出,并且如果它忽略 SIGINT(或将其用于
其他目的)。与 WUE 方式一样,shell 等待子进程
完全的。它计算程序是否在 SIGINT 时结束,如果
所以,它停止了脚本。如果程序执行任何其他退出,
脚本将继续。我将这种做事方式称为
本文档的其余部分使用“WCE”(“等待并合作退出”)。
我在 Bash 手册页中找不到对此的引用,但我会继续查找信息文档。但我 99% 确信这是正确答案。
旧答案:
Bash 脚本中命令的非零退出状态不会终止程序。如果你做一个echo $?
after ./script2.sh
它将显示 130。您可以使用以下命令终止脚本set -e
正如 phs 所建议的那样。
$ help set
...
-e Exit immediately if a command exits with a non-zero status.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)