目前在查看Android源码时候发现有很多shell脚本,由于方便我自己去调试脚本,就去查找了下脚本调试工具bashdb,如果大家之前使用过gdb的调试工具可以很容易的掌握,下面是我对常用命令的使用心得分享,更详细的内容可以查看文章末尾提供的链接,有官网的参考文档可以给大家学习。
这里我的环境是Mac的安装步骤,其他平台安装请自行google。
注意:下载过程可能需要
bashdb<0> show alias
cont: continue | fin: finish | list>: list | h: help
q!: quit | !: history | x: examine | ev: eval
bash: shell | exit: quit | ed: edit | eval?: eval
?: help | W: watch | s: step | d: clear
where: backtrace | T: backtrace | sh: shell | c: continue
s-: step- | R: run | q: quit | b: break
s+: step+ | pr: print | n: next | a: action
bt: backtrace | restart: run | l: list
l>: list | ev?: eval | i: info
debug.sh
shell脚本准备调试#!/bin/bash
function print
{
for test in Alabama Alaska Arizona Arkansas California Colorado
do
echo The next state is $test
done
}
function getdate
{
echo date +'%Y-%m-%d'
}
echo "Hello $USER,"
echo "Today is $(getdate)"
bashdb <调试文件>
MartindeMacBook-Pro:android_shell martin$ bashdb debug.sh
bash debugger, bashdb, release 4.4-0.94
Copyright 2002-2004, 2006-2012, 2014, 2016-2017 Rocky Bernstein
This is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
(/Users/martin/Workspace/Linux/android_shell/debug.sh:13):
13: echo "Hello $USER,"
bashdb<0>
l
查看被调试的shell脚本(这里的l是指list单词)。在l
后面可以直接跟行号显示bashdb<0> l 行号
bashdb<1> l 1
1: #!/bin/bash
2: function print
3: {
4: for test in Alabama Alaska Arizona Arkansas California Colorado
5: do
6: echo The next state is $test
7: done
8: }
9: function getdate
10: {
如果shell脚本文件过长会以分页的方式显示,我们可以点击Enter
键进行翻页操作。
bashdb<2> i b
bashdb<2> i b
No breakpoints have been set.
上面表示没有打过任何断点。
bashdb<3> b 2
bashdb<3> b 6
Breakpoint 2 set in file /Users/martin/Workspace/Linux/android_shell/debug.sh, line 6.
bashdb<2> b <函数名>
bashdb<2> b getdate
Breakpoint 1 set in file /Users/martin/Workspace/Linux/android_shell/debug.sh, line 2.
bashdb<0> b if $test==Alabama
bashdb<2> l 1
1: #!/bin/bash
2: function print
3: {
4: for test in Alabama Alaska Arizona Arkansas California Colorado
5: do
6: echo The next state is $test
7: done
8: }
9: function getdate
10: {
bashdb<3> b 6 if$test==Alabama
Breakpoint 1 set in file /Users/martin/Workspace/Linux/android_shell/debug.sh, line 6.
bashdb<4> d 2
bashdb<8> i b
Num Type Disp Enb What
2 breakpoint keep y /Users/martin/Workspace/Linux/android_shell/debug.sh:2
3 breakpoint keep y /Users/martin/Workspace/Linux/android_shell/debug.sh:10
bashdb<9> d 10
Removed 1 breakpoint(s).
bashdb<10> i b
Num Type Disp Enb What
2 breakpoint keep y /Users/martin/Workspace/Linux/android_shell/debug.sh:2
bashdb<1> run
bashdb<9> run
Restarting with: /usr/local/bin/bashdb debug.sh
bash debugger, bashdb, release 4.4-0.94
Copyright 2002-2004, 2006-2012, 2014, 2016-2017 Rocky Bernstein
This is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
(/Users/martin/Workspace/Linux/android_shell/debug.sh:13):
13: echo "Hello $USER,"
bashdb<0> c
bashdb<0> c
Hello martin,
Today is date +%Y-%m-%d
Debugged program terminated normally. Use q to quit or R to restart.
bashdb<1> q
bashdb<1> q
bashdb: That's all, folks...
step over:如果正在调用一个子函数,不会跟到函数内部
bashdb<0> n
bashdb<0> n
Hello martin,
(/Users/martin/Workspace/Linux/android_shell/debug.sh:14):
14: echo "Today is $(getdate)"
step into:如果正在调用一个子函数,不会跟到函数内部
bashdb<0> s
bashdb<1> s
(/Users/martin/Workspace/Linux/android_shell/debug.sh:14):
14: echo "Today is $(getdate)"
getdate
step return:如果正在调用一个子函数,并且进入函数内部,会退出到喊出。
bashdb<(2)> finish
bashdb<(2)> finish
Today is date +%Y-%m-%d
Debugged program terminated normally. Use q to quit or R to restart.
用于打印当前变量的值
bashdb<6> pr <打印的变量>
Hello martin,
(/Users/martin/Workspace/Linux/android_shell/debug.sh:14):
14: echo "Today is $(getdate)"
bashdb<6> pr $USER
martin
监控当前变量的值,当有值发生改变的时候,就改变值。
bashdb<1> watch <变量名称不带$号>
bashdb<1> watch USER
0: ($USER)==martin arith: 0
bashdb<2> n