help-bash
[Top][All Lists]
Advanced

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

[Help-bash] The best way to make RETURN trap mimic EXIT trap upon receiv


From: Peng Yu
Subject: [Help-bash] The best way to make RETURN trap mimic EXIT trap upon receiving other signals
Date: Mon, 10 Dec 2018 13:14:58 -0600

Hi,

When EXIT is trapped, upon receiving other signals a script will run
the code trapped at EXIT.

But this is not the case for RETURN. I'd like the code trapped at
RETURN in a function be called upon other signals are received while
the function runs.

So far, I come up with the following code. Basically, I need to trap
the other signals by the command 'return', so that they will all
converge to RETURN. Is this the most appropriate way to do it? Thanks.

$ cat ./main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

function f {
trap 'echo Hello World1!' RETURN
trap 'return' INT
./script.sh
sleep 10
}

trap 'echo Hello World!' RETURN
f
$ cat script.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

trap "echo trap at SIGINT in $0" SIGINT
sleep 10
$ ./main.sh
^Ctrap at SIGINT in ./script.sh
Hello World1!

-- 
Regards,
Peng



reply via email to

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