Bash debugging experience

Source: Internet
Author: User

Bash is one of the most commonly used shells in Unix/Linux operating systems. It is very flexible and is exceptionally powerful in combination with awk and C ++.

The following uses a test script to describe how to debug using Bash:
Test. Sh

#!/bin/bashecho "----------------begin-----------------"awk '{sum+=1} END{print sum}' test.shMAX=3for ((i = 0; i < MAX; i++))do        loaddate=`date -d"-$i day" +%Y-%m-%d`        echo $loaddatedoneecho "----------------end-----------------"

1. Use bash-x

Bash-x prints all statements during script execution.
Like:

$ Bash-X test. Sh
+ 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 above annotations, bash-X can basically meet 80% of daily needs.

2. Set

Sometimes, our scripts are very complicated. Using bash-X to get too many outputs makes it difficult to find the required information.
Set can be used for local debugging. Add "Set-X" before the code to be debugged. Add "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-
2013-03-04
2013-03-03
---------------- End -----------------

3. Use the bash debugging tool bashdb (Bash debugger)

Bashdb is a GDB-like debugging tool. It is basically accessible to students who use GDB.
Bashdb can run breakpoint settings, variable viewing, and other common debugging operations
Bashdb needs to be installed separately
Use:
$ Bashdb -- debug test. Sh
Bash debugger, bashdb, release 4.2-0.8

Copyright 2002,200 3, 2004,200 6, 2007,200 8, 2009,201 0, 2011 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.

(/Home/work/code/test. sh: 3 ):
3: Echo "---------------- begin -----------------"
Bashdb <0> N # Next Step
---------------- Begin -----------------
(/Home/work/code/test. sh: 5 ):
5: awk '{sum + = 1} end {print sum}' test. Sh
Bashdb <1> L # list the top 10 lines of code
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 # 10th sets a breakpoint
Breakpoint 1 set in file/home/work/code/test. Sh, line 10.
Bashdb <3> C # continue running
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 # print the variable value
0
14: Echo "---------------- end -----------------"

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.