Shell script Debugging Tools--bashdb

Source: Internet
Author: User
Tags arithmetic

Bash is one of the most commonly used shells of the unix/linux operating system, and it is very flexible, and is exceptionally powerful with awk and C + +.


The following uses a test script to illustrate how to use bash debugging
test.sh

[Plain]View Plaincopy
    1. #!/bin/bash
    2. echo "----------------begin-----------------"
    3. awk ' {sum+=1} end{print sum} ' test.sh
    4. Max=3
    5. for ((i = 0; i < MAX; i++))
    6. Do
    7. Loaddate= ' date-d '-$i Day "+%y-%m-%d"
    8. Echo $loaddate
    9. Done
    10. echo "----------------End-----------------"

1. Using Bash-x

Bash-x Print out all statements during script execution
Like

$ bash-x test.sh

Breakpoint

+ Echo----------------begin-----------------
----------------Begin-----------------
+ awk ' {sum+=1} end{print sum} ' test.sh
14
+ max=3
+ ((i = 0))
+ ((I < MAX))
+ + Date '-d-0 Day ' +%y-%m-%d
+ loaddate=2013-03-05
+ Echo 2013-03-05
2013-03-05
+ ((i++))
+ ((I < MAX))
+ + Date '-d-1 Day ' +%y-%m-%d
+ loaddate=2013-03-04
+ Echo 2013-03-04
2013-03-04
+ ((i++))
+ ((I < MAX))
+ + Date '-d-2 Day ' +%y-%m-%d
+ loaddate=2013-03-03
+ Echo 2013-03-03
2013-03-03
+ ((i++))
+ ((I < MAX))
+ Echo----------------End-----------------
----------------End-----------------

With the comments on the bash-x, the basic can meet 80% of daily needs

2. Set

Sometimes, our scripts are very complex, and using bash-x to get too much output, it's hard to find the information we need.
The set can be debugged locally, preceded by a "set-x" code that needs to be debugged, followed by a "set +x" after the code to be debugged.
Like
Modify test.sh:
....
Set-x

awk ' {sum+=1} end{print sum} ' test.sh

Set +x

.....
Run:
----------------Begin-----------------
+ awk ' {sum+=1} end{print sum} ' test.sh
16
+ Set +x
2013-03-05
2013-03-04
2013-03-03
----------------End-----------------

3. Using the Bash Debug Tool bashdb (Bash Debugger)

Bashdb is a gdb-like debugging tool that uses GDB's classmates to use BASHDB basic accessibility
BASHDB can run common debugging operations such as breakpoint settings, variable view, etc.
Bashdb need to be installed separately
Use the following:
$ bashdb--debug test.sh
Bash debugger, BASHDB, release 4.2-0.8


Copyright 2002, 2003, 2004, 2006,------Rocky Bernstein
Software, covered by the GNU general public License
Welcome to change it and/or distribute copies of it under certain.


(/home/work/code/test.sh:3):
3:echo "----------------begin-----------------"
Bashdb<0> N #下一步

----------------Begin-----------------

(/home/work/code/test.sh:5):
5:awk ' {sum+=1} end{print sum} ' test.sh
Bashdb<1> L #列出上下共10行代码
1: #!/bin/bash
2:
3:echo "----------------begin-----------------"
4:
5: = = awk ' {sum+=1} end{print sum} ' test.sh
6:
7:max=3
8:for ((i = 0; i < MAX; i++))
9:do
10:loaddate= ' date-d '-$i Day "+%y-%m-%d"
Bashdb<2>b #第10行设置断点
Breakpoint 1 set in file/home/work/code/test.sh, line 10.
Bashdb<3> C #继续运行
14
Breakpoint 1 Hit (1 times).
(/HOME/WORK/CODE/TEST.SH:10):
10:loaddate= ' date-d '-$i Day "+%y-%m-%d"
Bashdb<4>Print $i #打印变量值
0
14:echo "----------------End-----------------"

2) Debug Tool-bashdb
Using the shell debugger bashdb, this is a gdb-like debugging tool that can perform many functions such as breakpoint settings for shell scripts, stepping, variable observation, and so on.

  Common commands for debug using BASHDB
1. List code and query code classes:
l   lists 10 rows below the current row
-   lists the preceding 10 lines of the line of code being executed
.   back to the line of code that is being executed
w  lists the code before and after the line that is being executed
/pat/  backward search Pat
? Pat Search forward Pat

2.debug control class:
H   Assist
Help command   GET command specific information
q  exit Bashdb
x Arithmetic expression   Calculate the value of the arithmetic expression and show it
!! Space shell command Arguments   EXECUTE shell command
Common commands for debug using BASHDB (cont.)
Control script Execution class:
n   executes the next statement, encounters a function, does not enter the function, Use function as black box
s n  executes n times, encounters function entry function inside
B line number n  set breakpoints at row number n
Del line n undo breakpoint at line number n
C line number n is executed to line number n
r  Restart
Finish execution to program last
Cond N expr conditional breakpoint

Url:

http://blog.csdn.net/adaptiver/article/details/7054729

http://blog.csdn.net/yfkiss/article/details/8636758

Shell script Debugging Tools--bashdb

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.