shell-trap

How do I trap EXIT in a solaris 11 shell script?

佐手、 提交于 2020-01-17 13:15:00
问题 man signal.h indicates there's no SIGEXIT in Solaris 11. How can I trap it in my shell scripts? Or how can I simulate the old behavior of traping SIGEXT? 回答1: Why are you looking to the C API manual for a shell feature ? You definitely can trap the SIGEXIT signal under Solaris 11 shells (at least ksh93, bash and sh). $ cat /etc/release Oracle Solaris 11.1 X86 Copyright (c) 1983, 2012, Oracle and/or its affiliates. All rights reserved. Assembled 19 September 2012 $ cat /tmp/z #!/bin/ksh trap

Idiomatic way to exit ksh while loop

假装没事ソ 提交于 2019-12-13 00:14:51
问题 I've the following 5 second timer which prints an asterisk for each second. timer () { i=1 trap 'i=5' INT while [[ $i -le 5 ]]; do sleep 1 printf "*" ((i+=1)) done } Somehow the trap chunk seems a little hackish and I wonder if there's a more correct way to interrupt the entire loop (not just the current sleep cycle). I've tried: trap 'print "foo"' INT at various locations inside and outside the function, but as I alluded to before, that just interrupts the current sleep cycle. 回答1: Perhaps I

Is trap EXIT required to execute in case of SIGINT or SIGTERM received?

不打扰是莪最后的温柔 提交于 2019-12-08 17:36:53
问题 I have a simple script trap 'echo exit' EXIT while true; do sleep 1; done and it behaves differently in different shells: $ bash tst.sh ^Cexit $ dash tst.sh ^C $ zsh tst.sh ^C $ sh tst.sh ^Cexit So I'm not sure about how it should operate and whether it is specified at all. 回答1: The EXIT trap isn't working the same way in every shell. A few examples: In dash and zsh it's only triggered by a regular exit from within the script. In zsh, if you trap a signal that would normally quit the

Exit after trap fires

强颜欢笑 提交于 2019-11-30 14:57:20
问题 Take this script #!/bin/sh fd () { echo Hello world exit } trap fd EXIT INT for g in {1..5} do echo foo sleep 1 done 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? 回答1: 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

Exit after trap fires

做~自己de王妃 提交于 2019-11-30 12:54:02
Take this script #!/bin/sh fd () { echo Hello world exit } trap fd EXIT INT for g in {1..5} do echo foo sleep 1 done 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? 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