现在的位置: 首页 > 综合 > 正文

do you want to debug bash shell using tool ? ( by quqi99 )

2014年01月29日 ⁄ 综合 ⁄ 共 2808字 ⁄ 字号 评论关闭

                                                                                  do you want to debug bash shell using tool ? ( by quqi99 )

作者:张华  发表于:2012-05-07
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明

( http://blog.csdn.net/quqi99 )

          do you want to debug bash shell using tool ? if so, you can use bashdb, bashdb is a command tool, if you familiar with emacs or ddd, you can also conbine them as your GUI tool.

     bellow is the steps:

      1) yum install bashdb
   
      2) one example

           vi /bak/tmp/test.sh
 
           #!/bin/bash

version="0.01";

fibonacci() {
    n=${1:?If you want the nth fibonacci number, you must supply n as the first parameter.}
    if [ $n -le 1 ]; then
    echo $n
    else
    l=`fibonacci $((n-1))`
    r=`fibonacci $((n-2))`
    echo $((l + r))
    fi
}

for i in `seq 1 10`
do
  result=$(fibonacci $i)
  echo "i=$i result=$result"
done

      3) begin to debug

[root@hua devstack-gate]# bash --debugger /bak/tmp/test.sh
bash debugger, bashdb, release 4.2-0.7

Copyright 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 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.

(/bak/tmp/test.sh:3):
3:    version="0.01";
bashdb<0> next
(/bak/tmp/test.sh:16):
16:    for i in `seq 1 10`
bashdb<1> list
 11:        r=`fibonacci $((n-2))`
 12:        echo $((l + r))
 13:        fi
 14:    }
 15:    
 16: => for i in `seq 1 10`
 17:    do
 18:      result=$(fibonacci $i)
 19:      echo "i=$i result=$result"
 20:    done
bashdb<2> print $1

bashdb<3> next
(/bak/tmp/test.sh:18):
18:      result=$(fibonacci $i)
bashdb<4> print $1

bashdb<5> print $i
1
bashdb<6> exit
[root@hua devstack-gate]# clear

[root@hua devstack-gate]# bash --debugger /bak/tmp/test.sh
bash debugger, bashdb, release 4.2-0.7

Copyright 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 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.

(/bak/tmp/test.sh:3):
3:    version="0.01";
bashdb<0> next
(/bak/tmp/test.sh:16):
16:    for i in `seq 1 10`
bashdb<1> list
 11:        r=`fibonacci $((n-2))`
 12:        echo $((l + r))
 13:        fi
 14:    }
 15:    
 16: => for i in `seq 1 10`
 17:    do
 18:      result=$(fibonacci $i)
 19:      echo "i=$i result=$result"
 20:    done
bashdb<2> print $i

bashdb<3> next
(/bak/tmp/test.sh:18):
18:      result=$(fibonacci $i)
bashdb<4> print $i
1
bashdb<5> next
(/bak/tmp/test.sh:19):
19:      echo "i=$i result=$result"
bashdb<6> print $result

bashdb<7> next
i=1 result=1
(/bak/tmp/test.sh:16):
16:    for i in `seq 1 10`
bashdb<8> next
(/bak/tmp/test.sh:18):
18:      result=$(fibonacci $i)
bashdb<9> print $result

bashdb<10> print $i
2
bashdb<11> break
Breakpoint 1 set in file /bak/tmp/test.sh, line 18.
bashdb<12> continue
i=2 result=1
Breakpoint 1 hit (1 times).
(/bak/tmp/test.sh:18):
18:      result=$(fibonacci $i)
bashdb<13> continue
i=3 result=2
Breakpoint 1 hit (2 times).
(/bak/tmp/test.sh:18):
18:      result=$(fibonacci $i)
bashdb<14> continue
i=4 result=3
Breakpoint 1 hit (3 times).
(/bak/tmp/test.sh:18):
18:      result=$(fibonacci $i)
bashdb<16> quit

抱歉!评论已关闭.