How to debug a Shell Script on Unix or Lunix

How to debug a Shell Script

Use -x option to debug a shell script

Run a shell script with -x option. This is valid for bash shell scripts as well.

$ ksh -x tesh.sh
$ ksh -v test.sh

Use of set builtin command

korn shell offers debugging options which can be turn on or off using set command.

=> set -x : Display commands and their arguments as they are executed.

=> set -v : Display shell input lines as they are read.

You can use above two command in shell script itself:

<span style="color: rgb(0, 0, 153); font-weight: bold;">#!/bin/ksh</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">clear</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;"># turn on debug mode</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">set -x</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">for f in *</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">do</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">   file $f</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">done</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;"># turn OFF debug mode</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">set +x</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">ls</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;"># more commands</span>

You can replace standard

#!/bin/ksh


with (for debugging)

#!/bin/ksh -xv

Use of intelligent DEBUG function

Add special variable _DEBUG. Set to `on’ when you need to debug a script:

_DEBUG="on"

Put the following function at the beginning of the script:

function DEBUG()<br />{<br /> [ "$_DEBUG" == "on" ] &&  $@ || :<br />}

Now wherever you need debugging simply use DEBUG function

DEBUG echo "File is $filename"

OR

DEBUG set -x

Cmd1

Cmd2

DEBUG set +x

When debugging done and before moving a script to production set _DEBUG to off

No need to delete debug lines.

_DEBUG="off" # set to anything but not to 'on'

Sample script:

<span style="color: rgb(0, 0, 153); font-weight: bold;">#!/bin/bash</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">_DEBUG="on"</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">function DEBUG()</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">{</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;"> [ "$_DEBUG" == "on" ] &&  $@ || :</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">}</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">DEBUG echo 'Reading files'</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">for i in *</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">do</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">  grep 'something' $i > /dev/null</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">  [ $? -eq 0 ] && echo "Found in $i file" || :</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">done</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">DEBUG set -x</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">a=2</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">b=3</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">c=$(( $a + $b ))</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">DEBUG set +x</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">echo "$a + $b = $c"</span><br />

Save and run the script:

$ ./script.sh

Output:

<span style="color: rgb(0, 0, 153); font-weight: bold;">Reading files</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">Found in xyz.txt file</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">+ a=2</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">+ b=3</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">+ c=5</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">+ DEBUG set +x</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">+ '[' on == on ']'</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">+ set +x</span><br style="color: rgb(0, 0, 153); font-weight: bold;" /><span style="color: rgb(0, 0, 153); font-weight: bold;">2 + 3 = 5</span><br />

Now set DEBUG to off

_DEBUG="off"

Run script:

$ ./script.sh

Output:

<span style="font-weight: bold; color: rgb(0, 0, 153);">Found in xyz.txt file</span><br style="font-weight: bold; color: rgb(0, 0, 153);" /><span style="font-weight: bold; color: rgb(0, 0, 153);">2 + 3 = 5</span><br />

You can also try to use DEBUG as an alias instead of function.

Links

Korn Shell Debugger by OReilly

Bash Debugger Project

Powered by ScribeFire.

Respond to this post