【发布时间】:2013-08-30 22:18:31
【问题描述】:
script1.sh:
#!/bin/bash
./script2.sh
echo after-script
script2.sh:
#!/bin/bash
function handler {
exit 130
}
trap handler SIGINT
while true; do true; done
当我从终端启动 script1.sh,然后使用 Ctrl+C 将 SIGINT 发送到其进程组时,信号被 script2.sh 捕获并当 script2.sh 终止时,script1.sh 打印“after-script”。但是,我希望 script1.sh 在调用 script2.sh 的行之后立即终止。为什么在这个例子中不是这样?
补充说明(编辑):
由于 script1.sh 和 script2.sh 在同一个进程组中,当 Ctrl+C kbd> 在命令行上被按下。这就是为什么我不希望 script2.sh 退出时 script1.sh 继续。
当 script2.sh 中的“trap handler SIGINT”行被注释掉时,script1.sh 会在 script2.sh 存在后立即退出。我想知道为什么它的行为会有所不同,因为 script2.sh 会产生相同的退出代码 (130)。
【问题讨论】:
-
也许使用
set -e?