Trap: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 38: Line 38:
This works, explain this:
This works, explain this:
</font>
</font>
<syntaxhighlight lang='bash'>
local chart_dir=blah
local chart_dir=blah
trap 'rm -rf '${chart_dir}'/tmpcharts; echo chart dir: ${chart_dir}' EXIT
trap 'rm -rf '${chart_dir}'/tmpcharts; echo chart dir: ${chart_dir}' EXIT
Line 53: Line 54:
For a list of signals that can be handled, see: {{Internal|Linux_Signals|Linux Signals}}
For a list of signals that can be handled, see: {{Internal|Linux_Signals|Linux Signals}}
Also see: {{Internal|Handling Signals in bash|Handling Signals in bash}}
Also see: {{Internal|Handling Signals in bash|Handling Signals in bash}}
=Only One Code Sequence (Latests) Executes=
If multiple code sequences are declared with <code>trap</code>, only the last one is executed. The following example:
<syntaxhighlight lang='bash'>
trap 'echo A' EXIT
trap 'echo B' EXIT
</syntaxhighlight>
produces:
<syntaxhighlight lang='text'>
B
</syntaxhighlight>


=Behavior on Being Invoked from Sub-Shells=
=Behavior on Being Invoked from Sub-Shells=

Revision as of 20:19, 20 December 2019

External

Internal

Overview

Trap is a facility to instruct bash to catch signals and execute code depending on the signal. A common usage in shell scripts is to prevent those scripts to exit untimely when users type keyboard abort sequences, but run cleanup code instead.

Example:

trap 'rm -f ./lock' EXIT

Global variables declared before the trap declaration are correctly resolved when present in a single-quote quoted string (even if single-quotes are used, the single quote semantics when used in bash command line is different from that in effect here). For example, the following code:

a=hello
trap 'echo ${a}' EXIT

produces:

hello

Variable resolution is done at the time of execution, not declaration, so the following:

trap 'echo ${a}' EXIT
a=hello

also produces:

hello

Local variables do not resolve at execution, as one would expect.

This works, explain this:

local chart_dir=blah
trap 'rm -rf '${chart_dir}'/tmpcharts; echo chart dir: ${chart_dir}' EXIT

produces:

blah



"EXIT" in the example above is not a Linux signal. Bash provides this psuedo-signal, which is executed when the script exits; this can be used to make sure that your script executes some cleanup on exit.

For a list of signals that can be handled, see:

Linux Signals

Also see:

Handling Signals in bash

Only One Code Sequence (Latests) Executes

If multiple code sequences are declared with trap, only the last one is executed. The following example:

trap 'echo A' EXIT
trap 'echo B' EXIT

produces:

B

Behavior on Being Invoked from Sub-Shells

If code is registered with trap to react to EXIT in a sub-shell, or in a function that is invoked in a sub-shell, then the registered code will be executed when the sub-shell, and not the top-level invoking shell, exists.

The following code:

$(trap 'echo "a" 1>&2' EXIT)
echo "b"

will display:

a
b

Note that the output should be sent to stderr in the trap code - if the output is sent to stdout, the output is lost, even if the code executes.

TODO

Reactive Wait Container

Investigate usefulness in case of a reactive wait container. Also see Docker Concepts - Container Exit.

CMD exec /bin/bash -c "trap : TERM INT; sleep infinity & wait"