Exit after trap fires

做~自己de王妃 提交于 2019-11-30 12:54:02

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

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 ... :-)

what about in redefining trap default?

#!/bin/sh

fd () {
   echo Hello world
   trap - EXIT
   exit 127
}

trap fd INT EXIT
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!