陷阱火灾后退出 [英] Exit after trap fires
问题描述
把这个剧本
#!/bin/sh
fd ()
{
echo Hello world
exit
}
trap fd EXIT INT
for g in {1..5}
do
echo foo
sleep 1
done
我想 FD
火一次,无论是从控制-C或脚本正常退出。然而,如果你按下Control-C将运行两次。我该如何解决这个问题?
I would like fd
to fire once, either from Control-C or if the script exits normally. However if you hit Control-C it will run twice. How can I fix this?
推荐答案
千万级联陷阱。 退出127
将运行退出
陷阱和出口code设置为127,这样你就可以说
Do cascading traps. exit 127
will run the EXIT
trap and set the exit code to 127, so you can say
#!/bin/sh
fd () {
echo Hello world
# No explicit exit here!
}
trap fd EXIT
trap 'exit 127' INT
我记得我从其他人的脚本与各种变通奋力您的问题几年后学习这种。在那之后,我注意到,一些教程做解释这种技术。但它不是如明确记载Bash的手册页恕我直言。 (或者它是不是当我需要它也许有些事情没有在15年内改变... ...: - )
I remember learning this from other people's scripts after struggling with various workarounds to your problem for several years. After that, I have noticed that some tutorials do explain this technique. But it is not documented clearly in e.g. the Bash manual page IMHO. (Or it wasn't when I needed it. Maybe some things don't change in 15 years ... :-)
这篇关于陷阱火灾后退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!