Is there a way to disable a trap within the trap handler?
I'd like to simplify some code by using the RETURN trap. my_func will return the value of my_command. The tmpfile will be cleaned up as my_func returns. This technique would allow me to avoid assigning a temp var to hold $? from my_func while I do cleanup.
However, I'm unable to reset the trap handler within the handler and cleanup is now invoked after every function return.
Ultimately what I really want to do is cleanup after my_command is invoked but still have it as the last command so the return value is implicit. Any suggested alternatives would be appreciated.
cleanup() { # generic cleanup w/ reset
"$@"
trap - RETURN
}
my_func() {
local -r tmpfile="/tmp/tmpfile"
trap "cleanup rm ${tmpfile}" RETURN
my_command -f ${tmpfile}
}
caller() {
if my_func ; then
do_success_ops
fi
}