help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Are all traps global to a script?


From: Eric Blake
Subject: Re: [Help-bash] Are all traps global to a script?
Date: Fri, 7 Dec 2018 14:18:34 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.1

On 12/7/18 2:00 PM, Peng Yu wrote:
Hi,

I see that traps seem to be all global. For example, if I set it in a
function, it will have an effect outside a function.

You're demonstrating that you may lack a fundamental understanding of how functions work. Functions are snippets of code that run in the context of the caller (if we disregard the special effects of 'local'), and have an impact to the environment the same as any other code run in that context.


What is the best way to make effectively different traps for different
function? Does bash have a native support for this?

The same way that you implement different traps for different portions of a shell script that does not use functions. You can use 'trap -p' to save off the previous trap definition, install your new trap, then when done with your code, call trap to set things back to the value you saved off earlier.

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

trap "echo 'Hello World1!'" INT
function f {
     trap "echo 'Hello World!'" INT
     echo 'in f'
}
f
sleep 10
echo 'after f'

Remember, this is EXACTLY the same as writing:

trap "echo 'Hello World1!'" INT
{
  trap "echo 'Hello World!'" INT
  echo 'in f'
}
sleep 10
echo 'after f'

(that is, instead of defining 'f' to be a snippet of shell code, then invoking 'f' to run that snippet of code, just run that snippet of code directly)

And if you can solve the problem of restoring the original trap without a shell function (which you can thanks to 'trap -p'), then you can solve it when shell functions are involved.

--
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org



reply via email to

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