linux sh : bashdb的安装和初步使用

前言

这周在学习linux脚本编程,调试时,用echo 或bash -x
检查语法用bash -n x.sh

在函数中,要用echo返回出参字符串,如果再用echo打印调试信息,将逻辑搞乱了。
当时就在想,linux工具这么强大,既然有gdb, 那就有脚本的调试器才对啊.

今天在看书时,看到了bashdb这个工具。
在debian下试过了,好使。
虽然没有gdb -tui 那么好使,也满意了。

bashdb中也有好多调试命令,进入bashdb后,用help命令可以看到命令列表。
对于脚本的初级调试,掌握几个命令就够用。

下载点

bashdb-4.4-0.94.tar.bz2.7z

实验

# backup /etc/apt/sources.list
cp /etc/apt/sources.list /etc/apt/sources.list.bk1
# add one row on the file(/etc/apt/sources.list)'s end for install bashdb
vi /etc/apt/sources.list
deb http://ftp.de.debian.org/debian wheezy main
wq
# update install source
aptitude update
# install bashdb
aptitude install bashdb
# bash doc
# http://www.gnu.org/software/bash/manual/bashref.html
# debug linux sh
# cd the .sh file dir, debug x.sh by bashdb
bashdb $(pwd)/main.sh
# bashdb's buildin command list
bashdb<0> help
Available commands:
  action     condition  edit     frame    load     run     source  unalias  
  alias      continue   enable   handle   next     search  step    undisplay
  backtrace  debug      eval     help     print    set     step-   untrace  
  break      delete     examine  history  pwd      shell   step+   up       
  clear      disable    export   info     quit     show    tbreak  watch    
  commands   display    file     kill     return   signal  trace   watche   
  complete   down       finish   list     reverse  skip    tty  
# list command
bashdb<1> help list
list[>] [LOC|.|-] [NUMBER] 
LOC is the starting location or dot (.) for current file and
line. Subsequent list commands continue from the last line
listed. Frame switching however resets the line to dot. LOC can be a
read-in function name or a filename and line number separated by a
colon, e.g /etc/profile:5
If NUMBER is omitted, use the LISTSIZE setting as a count. Use "set
listsize" to change this setting. If NUMBER is given and is less than
the starting line, then it is treated as a count. Otherwise it is
treated as an ending line number.
By default aliases "l>" and "list>" are set to list. In this case and
more generally when the alias ends in ">", rather than center lines
around LOC that will be used as the starting point.
Examples:
list .      # List centered around the curent frame line
list        # Same as above if the first time. Else start from where
            # we last left off.
list -      # list backwards from where we left off.
list> .     # list starting from the current frame line.
list  10 3  # list 3 lines centered around 10, lines 9-11
list> 10 3  # list lines 10-12
list  10 13 # list lines 10-13
list  10 -5 # list from lines to 5 lines before teh end of the file
list  /etc/profile:5  # List centered around line 5 of /etc/profile.
list  /etc/profile 5  # Same as above.
list usage  # list centered around function usage().
# 列出脚本源码-当前
l .
# 列出脚本源码-指定行数
l 0
# bashdb的命令和gdb兼容, 用gdb的命令就可以了
# n : 步过
# s : 步入
# 回车 : 执行上一条命令
# 打印变量
print $i_index
pr $i_index
# 指定行数下断点
break 18
# 用debian源提供的bashdb下断点有bug, continue后,断不下来
# 去官网上下载一个新的bashdb : https://sourceforge.net/projects/bashdb/files/bashdb/4.4-0.94/
tar xvf ./bashdb-4.4-0.94.tar.bz2
cd bashdb-4.4-0.94/
aptitude install autotools-dev
make clean
./configure
make
make check # 这步有2项通不过,不影响
# SKIP: test-file-with-spaces
# FAIL: test-sig
# SKIP: test-bug-loc
# FAIL: test-bug-set-e
aptitude remove bashdb
rm -rf /usr/share/bashdb/
make install
# 可以用新版bashdb重新调试脚本了
bashdb $(pwd)/main.sh
# 列出当前文件所有内容, 行数范围设置大点 从0~1000, 如果这个当前sh只有45行,就都显示全了
l 0 1000
# 对指定文件的指定行数下断点
break /home/dev/main.sh:35
# 对函数下断点
break func_show_title
# 继续 continue
c
# 断点列表
L
# 列出指定文件指定行数
l /home/dev/common/sh.prog_version:16
# 删除断点(指定断点号码, 用L命令看断点列表)
delete 1
# bashdb好像没有在调试中,设置变量值的控制语法
# 执行完一个函数或一个循环
finish
# 退出调试器
q
# 好像初步使用bashdb, 查的这些调试命令,已经够用了
# 看一本书上,人家调试就用echo 或 bash -x, 人家对脚本编程该有多熟练啊
# 如果是我,只有在没有条件的情况下才会用echo, 有利器为啥不用呢?

你可能感兴趣的:(Linux)