Unix: Debugging your scripts even more effectively

Last week's post provided some tips and tricks for debugging scripts. This week's provides some even better ones!

By  

In last week's posting, I mentioned the -x and -v options that can be used on the shebang line to turn on trace or verbose options so that, when you run your script, you see each command as it is being executed. Well, this isn't the only way that you can use these options. You can also turn them on and off at various points within your scripts. If you only want to see what's happening within one particular section or several sections of your script, surround that section or those sections of your script with set -x and set +x commands. These turn debugging on anf off just for those sections of code.

set -x		# turn debugging on
while [ $x -le $end ]; do
  number=$x
  if [ $padlen -gt 1 ]; then
    while [ ${#number} -lt $padlen ]; do number="0${number}"; done
  fi
  echo -n "$number"
  [ $x -lt $end ] && echo -n " "
  x=$(( $x + 1 ))
done
set +x

Taking this even a little further, you can set a debug variable (e.g., DEBUG="set -x") outside of your script and then insert $DEBUG at various place in your script. To do this, you would use a variable in your script. For example:

$DEBUG
while [ $x -le $end ]; do
...

When DEBUG isn't defined, this variable has no effect whatsoever. It's basically a blank line. When you set it outside of your script, on the other hand, it turns into a set -x command.

$ export DEBUG='set -x'
$ ./myscript
...

If you want to turn debugging on and off, you can set up two variables, maybe DEBUGON and DEBUGOFF, and use then both.

#!/bin/bash

echo Greeting, $USER

$DEBUGON
echo "what do you see?"
$DEBUGOFF

echo "That's enough for now"

Then

$ export DEBUGON='set -x'
$ export DEBUGOFF='set +x'

The benefit of this approach is that you don't have to edit any commands out of your script when you are ready to go production. The use of DEBUG, DEBUGON and DEBUGOFF have no effect unless you set the variables.

Another good thing to know is that you don't have to put a -x or -v on your shebang lines to use these features. So, you don't have to actually modify a script to do command tracing. Instead, you can do this:

$ bash -x myscript

You can also expand on the information that the -x option will provide by modifying your PS4 setting. PS4 defines the prompt that is displayed when you're using debug mode.

Photo Credit: 

flickr / louento.pix

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Spotlight on ...
Online Training

    Upgrade your skills and earn higher pay

    Readers to share their best tips for maximizing training dollars and getting the most out self-directed learning. Here’s what they said.

     

    Learn more

Operating SystemsWhite Papers & Webcasts

See more White Papers | Webcasts

Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Ask a Question