bash調試經驗

來源:互聯網
上載者:User

bash調試經驗 bash是Unix/Linux作業系統最常用的shell之一,它非常靈活,和awk、c++配合起來異常強大  www.2cto.com  以下使用一個測試指令碼來說明使用bash調試的方法  www.2cto.com  test.sh[plain] #!/bin/bash    echo "----------------begin-----------------"    awk '{sum+=1} END{print sum}' test.sh    MAX=3  for ((i = 0; i < MAX; i++))  do          loaddate=`date -d"-$i day" +%Y-%m-%d`          echo $loaddate  done    echo "----------------end-----------------"   1. 使用bash -xbash -x列印出指令碼執行過程中的所有語句like: $ bash -x test.sh + echo ----------------begin---------------------------------begin-----------------+ awk '{sum+=1} END{print sum}' test.sh14+ MAX=3+ (( i = 0 ))+ (( i < MAX ))++ date '-d-0 day' +%Y-%m-%d+ loaddate=2013-03-05+ echo 2013-03-052013-03-05+ (( i++ ))+ (( i < MAX ))++ date '-d-1 day' +%Y-%m-%d+ loaddate=2013-03-04+ echo 2013-03-042013-03-04+ (( i++ ))+ (( i < MAX ))++ date '-d-2 day' +%Y-%m-%d+ loaddate=2013-03-03+ echo 2013-03-032013-03-03+ (( i++ ))+ (( i < MAX ))+ echo ----------------end---------------------------------end-----------------配合上注釋,bash -x基本可以滿足日常80%的需求 2. set 有的時候,我們的指令碼非常複雜,使用bash -x得到的輸出太多,很難找到需要的資訊set 可以進行局部調試,在需要調試的代碼之前加上“set -x”,需要調試的代碼之後加上“set +x”即可like:修改test.sh:....set -xawk '{sum+=1} END{print sum}' test.shset +x.....運行:----------------begin-----------------+ awk '{sum+=1} END{print sum}' test.sh16+ set +x2013-03-052013-03-042013-03-03----------------end----------------- 3. 使用bash調試工具bashdb(Bash Debugger)bashdb是一個類GDB的調試工具,使用GDB的同學使用bashdb基本無障礙bashdb可以運行斷點設定、變數查看等常見調試操作bashdb需要單獨安裝使用如下:$ bashdb --debug test.sh            bash debugger, bashdb, release 4.2-0.8  Copyright 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011 Rocky BernsteinThis is free software, covered by the GNU General Public License, and you arewelcome to change it and/or distribute copies of it under certain conditions.  (/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.shbashdb<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 #第10行設定斷點Breakpoint 1 set in file /home/work/code/test.sh, line 10.bashdb<3> c #繼續運行14Breakpoint 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-----------------"

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.