* How to trap to script EXIT instead of function EXIT?
@ 2018-12-03 4:09 Peng Yu
2018-12-03 7:41 ` Bart Schaefer
0 siblings, 1 reply; 4+ messages in thread
From: Peng Yu @ 2018-12-03 4:09 UTC (permalink / raw)
To: zsh-users
Hi,
The following example shows that `trap` inside a function is trapped
to the function. This behavior is different from bash. Is there a way
to make it the same as bash?
$ cat main.sh
#!/usr/bin/env zsh
# vim: set noexpandtab tabstop=2:
set -v
function f {
trap "echo 'Hello World!'" EXIT
trap
}
f
trap
$ ./main.sh
function f {
trap "echo 'Hello World!'" EXIT
trap
}
f
trap -- 'echo '\''Hello World!'\' EXIT
Hello World!
trap
$ cat ./main.bash
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:
set -v
function f {
trap "echo 'Hello World!'" EXIT
trap
}
f
trap
$ ./main.bash
function f {
trap "echo 'Hello World!'" EXIT
trap
}
f
trap -- 'echo '\''Hello World!'\''' EXIT
trap
trap -- 'echo '\''Hello World!'\''' EXIT
echo 'Hello World!'
Hello World!
--
Regards,
Peng
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: How to trap to script EXIT instead of function EXIT?
2018-12-03 4:09 How to trap to script EXIT instead of function EXIT? Peng Yu
@ 2018-12-03 7:41 ` Bart Schaefer
2018-12-03 14:05 ` Peng Yu
0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2018-12-03 7:41 UTC (permalink / raw)
To: pengyu.ut; +Cc: Zsh Users
On Sun, Dec 2, 2018 at 8:10 PM Peng Yu <pengyu.ut@gmail.com> wrote:
>
> The following example shows that `trap` inside a function is trapped
> to the function. This behavior is different from bash. Is there a way
> to make it the same as bash?
setopt posixtraps
Or more generally, "emulate sh".
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: How to trap to script EXIT instead of function EXIT?
2018-12-03 7:41 ` Bart Schaefer
@ 2018-12-03 14:05 ` Peng Yu
2018-12-03 14:59 ` Peter Stephenson
0 siblings, 1 reply; 4+ messages in thread
From: Peng Yu @ 2018-12-03 14:05 UTC (permalink / raw)
To: schaefer; +Cc: zsh-users
On Mon, Dec 3, 2018 at 1:41 AM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> On Sun, Dec 2, 2018 at 8:10 PM Peng Yu <pengyu.ut@gmail.com> wrote:
> >
> > The following example shows that `trap` inside a function is trapped
> > to the function. This behavior is different from bash. Is there a way
> > to make it the same as bash?
>
> setopt posixtraps
>
> Or more generally, "emulate sh".
Thanks.
If function f1 calls function f2, I want to call trap in f2 which
actually set the trap for f1. Is it possible?
--
Regards,
Peng
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: How to trap to script EXIT instead of function EXIT?
2018-12-03 14:05 ` Peng Yu
@ 2018-12-03 14:59 ` Peter Stephenson
0 siblings, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2018-12-03 14:59 UTC (permalink / raw)
To: zsh-users
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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2018-12-03 15:00 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-03 4:09 How to trap to script EXIT instead of function EXIT? Peng Yu
2018-12-03 7:41 ` Bart Schaefer
2018-12-03 14:05 ` Peng Yu
2018-12-03 14:59 ` Peter Stephenson
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).