Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 

I just stumbled upon a nice way to enhance the bash debug trace output format (See here for the source).

 

Just declare

declare -r PS4='|${LINENO}> \011${FUNCNAME[0]:+${FUNCNAME[0]}(): }'

and you will get following output

framp@obelix:~/scripts$ bash -x ./dbg.sh 3
+ declare -r 'PS4=|${LINENO}> \011${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
|16> main(): fib 3
|6> fib(): (( 3 <= 0 ))
|8> fib(): (( 3 == 1 ))
||11> fib(): fib 1
||6> fib(): (( 1 <= 0 ))
||8> fib(): (( 1 == 1 ))
||9> fib(): echo 1
||11> fib(): fib 2
||6> fib(): (( 2 <= 0 ))
||8> fib(): (( 2 == 1 ))
|||11> fib(): fib 0
|||6> fib(): (( 0 <= 0 ))
|||7> fib(): echo 0
|||11> fib(): fib 1
|||6> fib(): (( 1 <= 0 ))
|||8> fib(): (( 1 == 1 ))
|||9> fib(): echo 1
||11> fib(): echo 1
|11> fib(): echo 2
2

when you execute following code to calculate the Fibonacci numbers:

1: #!/bin/bash

3: declare -r PS4='|${LINENO}> \011${FUNCNAME[0]:+${FUNCNAME[0]}(): }'

5: function fib(){
6: if (( $1 <= 0 )); then
7: echo 0
8: elif (( $1 == 1 )); then
9: echo 1
10: else
11: echo $[ $(fib $(($1-2)) ) + $(fib $(($1 - 1)) ) ]
12: fi

14: }

16: fib $1
 

 

Add comment

*** Note ***

Comments are welcome. But in order to reject spam posts please consider following rules:
  1. Comments with string http are rejected with message You have no rights to use this tag
  2. All comments are reviewed by hand and thus it usually takes one day until a comment will be published.