Debugging Variables

Bash 3.0 added some useful environment variables to aid in writing a debugger. These include BASH_SOURCE, which contains an array of filenames that correspond to what is currently executing; BASH_LINENO, which is an array of line numbers that correspond to function calls that have been made; BASH_ARGC and BASH_ARGV array variables, the first holding the number of parameters in each frame and the second the parameters themselves.

We'll now look at writing a debugger, although we'll keep things simple and avoid using these variables. This also means the debugger will work with earlier versions of bash.

广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元

 


[1] Actually, if you are really concerned about efficiency, there are shell code compilers on the market; they convert shell scripts to C code that often runs quite a bit faster.

[2] In versions of bash prior to 2.0, LINENO won't give you the current line in a function. LINENO, instead, gives an approximation of the number of simple commands executed so far in the current function.

[3] We should admit that if you had turned on the nounset option at the top of this script, the shell would have flagged this error.

[6] You can use this signal only for the exiting of a script. Functions don't generate the EXIT signal, as they are part of the current shell invocation.

[7] Inheritance of the ERR trap is not available in versions of bash prior to 3.0.

[8] Warning: the DEBUG trap was run after statements in versions of bash prior to 2.05b. The debugger in this chapter has been written for the current version of bash where the trap is run before each statement.

[9] Inheritance of the DEBUG trap, declare -t, set -o functrace, and set -T are not available in bash prior to version 3.0.