How to add debugging to shellscripts

Debugging bash scripts is pretty straightforward, throwing around a couple echo and set -x quickly gives you what you need. But what if you want to add a nice breakpoint,  debugging to lots of paces in the code or turn all debugging on or off at once? Then this little script I wrote is the right thing for you: debug.sh just download it to the same directory as your script and include it with the following line:

Select All Code:
. debug.sh

It contains 4 simple functions that will make your bash coding easier.
debug and breakpoint both print the argument with a timestamp to STDERR
You can turn off all the functions by adding a DEBUG=false into your code

Example:

Select All Code:
#!/bin/bash
. debug.sh
 
debug_on
echo foo
debug_off
 
debug "I am a test"
 
breakpoint "wait for a bit"
 
echo done

Output:

Select All Code:
+ echo foo
foo
+ debug_off
+ true
+ set +x
[16:37:47] I am a test
[16:37:47] -breakpoint- wait for a bit
press any key to continue
 
done

Trackback URL

,

No Comments on "How to add debugging to shellscripts"

Hi, leave a comment:


ALLOWED XHTML TAGS:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Subscribe to Comments