深入探索Bash脚本的高级特性
1. 捕获中断信号
在Bash脚本中, trap 命令可用于捕获特定信号并执行相应操作。例如:
trap "trapped INT" INT
trap "trapped KILL" KILL # This won't actually work
trap "trapped QUIT" QUIT
trap "trapped TERM" TERM
trap "trapped USR1" USR1 # This one is special
# Just hang out and do nothing, without introducing "third-party"
# trap behavior, such as if we used 'sleep'
while (( 1 )); do
: # : is a NOOP
done
当我们运行这个脚本并尝试终止它时,会发现部分信号被成功捕获:
$ ./hard_to_kill
^CReceived INT trap--neener, neener
^CReceived INT trap--neener, neener
^CReceived INT trap--neener, neener
^Z
[1]+ Stopped ./hard_to_kill
$ kill -TERM %1
[1]+ Stopped ./hard_to_
超级会员免费看
订阅专栏 解锁全文
58

被折叠的 条评论
为什么被折叠?



