1 |
#!/usr/bin/env bash |
2 |
# |
3 |
# Test call stack introspection. There are a bunch of special variables |
4 |
# defined here: |
5 |
# |
6 |
# https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html |
7 |
# |
8 |
# - The shell function ${FUNCNAME[$i]} is defined in the file |
9 |
# ${BASH_SOURCE[$i]} and called from ${BASH_SOURCE[$i+1]} |
10 |
# |
11 |
# - ${BASH_LINENO[$i]} is the line number in the source file |
12 |
# (${BASH_SOURCE[$i+1]}) where ${FUNCNAME[$i]} was called (or |
13 |
# ${BASH_LINENO[$i-1]} if referenced within another shell function). |
14 |
# |
15 |
# - For instance, ${FUNCNAME[$i]} was called from the file |
16 |
# ${BASH_SOURCE[$i+1]} at line number ${BASH_LINENO[$i]}. The caller builtin |
17 |
# displays the current call stack using this information. |
18 |
# |
19 |
# So ${BASH_SOURCE[@]} doesn't line up with ${BASH_LINENO}. But |
20 |
# ${BASH_SOURCE[0]} does line up with $LINENO! |
21 |
# |
22 |
# Geez. |
23 |
|
24 |
|
25 |
### ${FUNCNAME[@]} array |
26 |
g() { |
27 |
argv.py "${FUNCNAME[@]}" |
28 |
} |
29 |
f() { |
30 |
argv.py "${FUNCNAME[@]}" |
31 |
g |
32 |
argv.py "${FUNCNAME[@]}" |
33 |
} |
34 |
f |
35 |
## STDOUT: |
36 |
['f'] |
37 |
['g', 'f'] |
38 |
['f'] |
39 |
## END |
40 |
|
41 |
### ${BASH_SOURCE[@]} is a stack of source files for function calls |
42 |
$SH spec/testdata/bash-source.sh |
43 |
## STDOUT: |
44 |
['begin F funcs', 'f', 'main'] |
45 |
['begin F files', 'spec/testdata/bash-source.sh', 'spec/testdata/bash-source.sh'] |
46 |
['begin F lines', '21', '0'] |
47 |
['G funcs', 'g', 'f', 'main'] |
48 |
['G files', 'spec/testdata/bash-source-2.sh', 'spec/testdata/bash-source.sh', 'spec/testdata/bash-source.sh'] |
49 |
['G lines', '15', '21', '0'] |
50 |
['end F funcs', 'f', 'main'] |
51 |
['end F', 'spec/testdata/bash-source.sh', 'spec/testdata/bash-source.sh'] |
52 |
['end F lines', '21', '0'] |
53 |
## END |
54 |
|
55 |
### ${BASH_LINENO[@]} is a stack of line numbers for function calls |
56 |
# note: it's CALLS, not DEFINITIONS. |
57 |
g() { |
58 |
argv.py G "${BASH_LINENO[@]}" |
59 |
} |
60 |
f() { |
61 |
argv.py 'begin F' "${BASH_LINENO[@]}" |
62 |
g # line 6 |
63 |
argv.py 'end F' "${BASH_LINENO[@]}" |
64 |
} |
65 |
f # line 9 |
66 |
## STDOUT: |
67 |
['begin F', '9'] |
68 |
['G', '6', '9'] |
69 |
['end F', '9'] |
70 |
## END |
71 |
|
72 |
### $LINENO is the current line, not line of function call |
73 |
g() { |
74 |
argv.py $LINENO # line 2 |
75 |
} |
76 |
f() { |
77 |
argv.py $LINENO # line 5 |
78 |
g |
79 |
argv.py $LINENO # line 7 |
80 |
} |
81 |
f |
82 |
## STDOUT: |
83 |
['5'] |
84 |
['2'] |
85 |
['7'] |
86 |
## END |