shell脚本调试工具的安装和使用

前言

目前在查看Android源码时候,发现有很多shell脚本,由于方便我自己去调试脚本,就去查找了下脚本调试工具bashdb,如果大家之前使用过gdb的调试工具可以很容易的掌握,下面是我的一点使用心得,希望可以给大家分享,更详细的内容可以去文章末尾,有官网的参考文档可以给大家学习。

1.安装

这里我的环境是Mac的安装步骤,其他平台安装请自行google。

注意:下载过程可能需要翻墙

2.使用bashdb

2.1 准备调试脚本

  • 创建debug.shshell脚本准备调试
#!/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)"

2.2 进入调试模式

  • 命令
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>

2.3 查看程序

  • 命令
    在待输入光标区域输入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键进行翻页操作。

2.4 断点相关操作

2.4.1查看断点信息

  • 命令
    在带输入区域输入i b(这里是info break的缩写)。
bashdb<2>  i b
  • 日志
bashdb<2>  i b
No breakpoints have been set.

上面表示没有打过任何断点。

2.4.2 b(reak) 行号

  • 命令
bashdb<3> b 2
  • 日志
bashdb<3> b 6
Breakpoint 2 set in file /Users/martin/Workspace/Linux/android_shell/debug.sh, line 6.

2.4.3 b(reak) 函数名

  • 命令
bashdb<2> b <函数名>
  • 日志
bashdb<2> b getdate
Breakpoint 1 set in file /Users/martin/Workspace/Linux/android_shell/debug.sh, line 2.

2.4.4 b(reak) 行号 if 条件

  • 命令
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.

2.5 删除断点

2.5.1 d(elete)

  • 命令
    在删除断点的时候,我们需要先查看一下当前断点的所在行号,然后删除断点行号即可。
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

2.3 运行

r(run):运行脚本

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,"

c(ontinue):继续运行到下一个断点

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.

q(quit):退出当前程序

  • 命令
bashdb<1> q
  • 日志
bashdb<1> q
bashdb: That's all, folks...

2.5 单步调试

n(next)

step over:如果正在调用一个子函数,不会跟到函数内部

  • 命令
bashdb<0> n
  • 日志
bashdb<0> n
Hello martin,
(/Users/martin/Workspace/Linux/android_shell/debug.sh:14):
14:	echo "Today is $(getdate)"

s(step)

step into:如果正在调用一个子函数,不会跟到函数内部

  • 命令
bashdb<0> s
  • 日志
bashdb<1> s
(/Users/martin/Workspace/Linux/android_shell/debug.sh:14):
14:	echo "Today is $(getdate)"
getdate

f(finish)

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.

参考链接

猜你喜欢

转载自blog.csdn.net/u011195398/article/details/88667111