Tuesday, August 21, 2012

How could you debug specific lines in shell script intelligently?

We can implement an intelligent DEBUG-function to debug a script at specified line by putting the following function at the beginning of the script:

_DEBUG="on"
function DEBUG()
{
 [ "$_DEBUG" == "on" ] &&  $@
}

When done with debugging  or before moving your script to production, set "_DEBUG" to 'off'.
 _DEBUG="off"  # set to anything but not to 'on'

For Example:

#!/bin/bash

_DEBUG="on"
function DEBUG()
{
[ "$_DEBUG" == "on" ] && $@
}

DEBUG echo 'Hi!'

No comments :