Is there any way to debug a bash script? E.g something that prints a sort of execution log like "calling line 1", "calling line 2" etc.
相关问题
- How to get the return code of a shell script in lu
- JQ: Select when attribute value exists in a bash a
- Invoking Mirth Connect CLI with Powershell script
- Emacs shell: save commit message
- bash print whole line after splitting line with if
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- In IntelliJ IDEA, how can I create a key binding t
- Check if directory exists on remote machine with s
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
There's good amount of detail on logging for shell scripts via global varaibles of shell. We can emulate the similar kind of logging in shell script: http://www.cubicrace.com/2016/03/log-tracing-mechnism-for-shell-scripts.html
The post has details on introdducing log levels like INFO , DEBUG, ERROR. Tracing details like script entry, script exit, function entry, function exit.
Sample log:
This answer is valid and useful: https://stackoverflow.com/a/951352
But, I find that the "standard" script debugging methods are inefficient, unintuitive, and hard to use. For those used to sophisticated GUI debuggers that put everything at your fingertips and make the job a breeze for easy problems (and possible for hard problems), these solutions aren't very satisfactory.
What I do is use a combination of DDD and bashdb. The former executes the latter, and the latter executes your script. This provides a multi-window UI with the ability to step through code in context and view variables, stack, etc., without the constant mental effort to maintain context in your head or keep re-listing the source.
There is guidance on setting that up here: http://ubuntuforums.org/showthread.php?t=660223
Some trick to debug bash scripts:
Using
set -[nvx]
In addition to
and
for stopping dump.
I would like to speak about
set -v
wich dump as smaller as less developped output.Dump variables or tracing on the fly
For testing some variables, I use sometime this:
for adding:
at line 18 and running resulting script (with args), without having to edit them.
of course, this could be used for adding
set [+-][nvx]
:will add
declare -p v1 v2 >&2
after line 18,set -x
before line 22 andset +x
before line 26.little sample:
Note: Care about
$LINENO
will be affected by on-the-fly modifications!( To see resulting script whithout executing, simply drop
bash <(
and) arg1 arg2
)Step by step, execution time
Have a look at my answer about how to profile bash scripts
I think you can try this Bash debugger: http://bashdb.sourceforge.net/.
You can also write "set -x" within the script.
I built a Bash debugger. Just give it a try. I hope it will help https://sourceforge.net/projects/bashdebugingbash