help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

reset a trap upon use


From: bill-auger
Subject: reset a trap upon use
Date: Fri, 24 Nov 2023 11:08:03 -0500

#!/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 ; }

prints:
  main()
  setup()
  helper()
  cleanup() caller=setup
  cleanup() caller=setup
  helper()
  cleanup() caller=main

is this the expected behavior? - should return from main() trigger the trap,
after cleanup() has reset it? - should return from helper() _not_ trigger the 
trap?

in case it is not obvious, the intention is for the trap to certainly fire once,
but only once - i have found several ways to accomplish that - all of which 
place
the trap reset outside of the cleanup() function

this works, for example:

setup() { echo "setup" ; trap 'cleanup ; trap - INT TERM RETURN ;' INT TERM 
RETURN ; helper ; }

just wanted to know if this is the expected behavior (or did my predecessor 
write a bug
by placing the trap reset inside of the cleanup() function)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]