2011-09-14 29 views
7

如果我運行慶典:調試選項和功能

bash -x myscript.sh 

我去拿調試輸出。

但是,如果我在myscript.sh中有函數,函數中的代碼不受-x選項的影響。它寫入僅輸出函數的名稱。

如何獲取bash腳本中函數的調試輸出?

更新:

繼ztank1013的反應,我才意識到,我用KSH,不bash的。似乎bash默認啓用了我的系統中的functrace選項(感謝bash-o-logist)

我很滿意,但對於社區,我保持打開ksh的問題。

腳本:

#!/bin/ksh 

a=2 
testering(){ 
     a=3 
     if [ $a -eq 3 ]; then 
       echo lili 
     fi 
} 
if [ $a -eq 2 ]; then 
     echo mimi 
fi 

testering 
exit 

ksh -x ./testdebug.sh輸出爲:

+ a=2 
+ [ 2 -eq 2 ] 
+ echo mimi 
mimi 
+ testering 
lili 
+ exit 

所以,KSH,有什麼訣竅?

(如果沒有答案會來的,「正確的」必去抨擊鄰LOGIST。)

+0

很好的問題,+1 – nsd

回答

8

使用bash ,你可以使用functrace選項在腳本

set -o functrace 

見聯機幫助頁bash用於其他調試器選項。

4

給我的測試腳本(debug.sh)我無法重現你的問題,其實:

[root ~]# cat debug.sh 
#!/bin/bash 
fun01() { 
echo "BUT HERE I am inside the function fun01() body" 
} 
echo "HERE I am outside the function fun01() body!" 
sleep 2 
fun01 
exit 

我與調試選項來運行它關閉:

[root ~]# ./debug.sh 
HERE I am outside the function fun01() body! 
BUT HERE I am inside the function fun01() body 

並打開(bash的-x ...):

[root ~]# bash -x ./debug.sh 
+ echo 'HERE I am outside the function fun01() body!' 
HERE I am outside the function fun01() body! 
+ sleep 2 
+ fun01 
+ echo 'BUT HERE I am inside the function fun01() body' 
BUT HERE I am inside the function fun01() body 
+ exit 

據我所見,在fun01()函數內部執行的行顯示了一個起始+這是調試器的實際操作。

@慶典鄰LOGIST即使我添加變量或的if/then/else條件構建我仍然得到所有調試信息:

[[email protected] ~]# cat debug-new.sh 
#!/bin/bash 
fun01() { 
INSIDEVAR='Never really use this one' 
echo "BUT HERE I am inside the function fun01() body" 
if [ true ] ; then echo 'this is going to be printed always!' ; fi 
} 
echo "HERE I am outside the function fun01() body!" 
sleep 2 
fun01 
exit 

再次執行:

[[email protected] ~]# bash -x debug-new.sh 
+ echo 'HERE I am outside the function fun01() body!' 
HERE I am outside the function fun01() body! 
+ sleep 2 
+ fun01 
+ INSIDEVAR='Never really use this one' 
+ echo 'BUT HERE I am inside the function fun01() body' 
BUT HERE I am inside the function fun01() body 
+ '[' true ']' 
+ echo 'this is going to be printed always!' 
this is going to be printed always! 
+ exit 
+1

你不能只在函數內部回顯一些語句。嘗試聲明變量並在函數中使用if/else。 –

+0

+1幫助我 –

2

KSH中使用typeset -ft function-name追蹤到函數

+0

如何使用這個命令?我在腳本里試過,但機智沒有成功...... –

+0

@FlorinGhita在腳本里面,用正在調試的函數的名字替換*'function-name' *。 – yarek

0

我有類似的問題,我在寫我自己的debbuger爲猛砸結束。嘗試一下! ...我希望它能幫助你https://sourceforge.net/projects/bashdebugingbash/

+0

在此處發佈您的代碼,而不是指向您的代碼的鏈接。 –

+0

截圖看起來很有希望,但不起作用 ''' 1的/ usr /共享/ BDB $ PWD 在/ usr /共享/ BDB 1的/ usr /共享/ BDB $ ./bdb.sh +爲bdbINC在INC/bdb.lang inc/bdb.vars inc/bdb.lib + INCFICH =/usr/share/bdb/inc/bdb.lang + typeset -u INCUPR = inc/bdblang + INCFLAG = INC/BDBLANG_INCLUDE ./bdb .sh:第29行:INC/BDBLANG_INCLUDE:錯誤替換 +退出 ''' 和我的西班牙語太糟糕了,無法理解您的代碼 –