blob: 7700c98ac34c668ae15fb207313aded3dbb42c60 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#!/bin/bash
#
# Usage:
#
# finally <eval-string> <cmd> [args...]
#
# What it does:
#
# (1) First, the <cmd> is executed with its arguments if any.
#
# (2) Then <eval-string> is executed as it would be by eval in
# the most global scope of the shell. (It is actually sent to
# the bash prompt's standard input as a list of quoted words.)
#
# Reason to exist:
#
# If execution of <cmd> is interrupted by signal, the
# <eval-string> will still run.
finally()
{
exec \
{FINALLY_0}<&0 \
{FINALLY_1}>&1 \
{FINALLY_2}>&2 \
0< <(finally_unwind "$1") \
&> /dev/null # silence prompt
# note that finally_unwind still has un-redirected
# stdin/stderr
{
"${@:2}"
} <&$FINALLY_0 >&$FINALLY_1 2>&$FINALLY_2
}
finally_unwind()
{
case $# in
0 )
exec \
0<&$FINALLY_0 \
1>&$FINALLY_1 \
2>&$FINALLY_2 \
{FINALLY_0}<&- \
{FINALLY_1}>&- \
{FINALLY_2}>&-
;;
1 )
echo "history -d -1; finally_unwind; $1"
;;
esac
}
|