Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: How to trap to script EXIT instead of function EXIT?



On Mon, 2018-12-03 at 08:05 -0600, Peng Yu wrote:
> If function f1 calls function f2, I want to call trap in f2 which
> actually set the trap for f1. Is it possible?

Obviously, this is incompatible with the POSIX-style trap usage.


f1() {
  f2() {
    trap 'trap '\''print Was in $lastfunc'\'' EXIT' EXIT 
    lastfunc=f2
  }
  f2
  lastfunc=f1
}


You can neaten this with a bit of variable syntax...


1() {
  f2() {
    local -a trap=(trap 'print Was in $lastfunc' EXIT)
    trap=(${(q)trap})
    trap "$trap" EXIT
    lastfunc=f2
  }
  f2
  lastfunc=f1
}


pws



Messages sorted by: Reverse Date, Date, Thread, Author