[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: reset a trap upon use
From: |
Greg Wooledge |
Subject: |
Re: reset a trap upon use |
Date: |
Fri, 24 Nov 2023 11:25:05 -0500 |
On Fri, Nov 24, 2023 at 11:08:03AM -0500, bill-auger wrote:
> #!/bin/bash
>
> if i reset a trap within a helper function, or in a function called by the
> trap,
> the trap is not reset
>
> setup() { echo "setup()" ; trap 'cleanup' INT TERM RETURN ; helper ;
> cleanup ; }
> helper() { echo "helper()" ; }
> cleanup() { echo "cleanup() caller=${FUNCNAME[1]}" ; trap - INT TERM RETURN ;
> }
> main() { echo "main()" ; setup ; helper ; }
I wonder what you're actually trying to do. Bash (but not sh in general)
allows you to set an EXIT trap which is perfectly suited for running a
cleanup function when the scripts exits, either normally or due to a
fatal signal.
#!/bin/bash
cleanup() {
rm stuff etc.
}
trap cleanup EXIT
Anything more complex than this is likely to fail in subtle ways due
to race conditions. Using an EXIT trap instead of a SIGINT trap also
lets the script exit properly upon receipt of SIGINT, avoiding many of
the issues documented at <http://www.cons.org/cracauer/sigint.html>.
Did you actually need anything more than a cleanup-on-exit function?