Previous Section  < Day Day Up >  Next Section

12.11. Debugging

By turning on the noexec option or using the –n argument to the ksh command, you can check the syntax of your scripts without really executing any of the commands. If there is a syntax error in the script, the shell will report the error. If there are no errors, nothing is displayed.

The most commonly used method for debugging scripts is to turn on the xtrace option or to use the ksh command with the –x option. These options allow an execution trace of your script. Each command from your script is displayed after variable substitution has been performed, and then the command is executed. When a line from your script is displayed, it is preceded with the value of the PS4 prompt, a plus (+) sign. The PS4 prompt can be changed.

With the verbose option turned on, or by invoking the Korn shell with the –v option (ksh –v scriptname), each line of the script will be displayed, just as it was typed in the script, and then executed. See Table 12.14 for debug commands.

Example 12.66.

(The Script)

     #!/bin/ksh

     # Scriptname: todebug

1    name="Joe Blow"

2    if [[ $name = [Jj]* ]] then

         print Hi $name

     fi

     num=1

3    while (( num < 5 ))

     do

4         (( num=num+1 ))

     done

5    print The grand total is $num



(The Output)

1    $ ksh –x todebug

2    + name=Joe Blow

     + [[ Joe Blow = [Jj]* ]]

     + print Hi Joe Blow

     Hi Joe Blow

     + num=1       The + is the PS4 prompt

     + let num < 5

     + let num=num+1

     + let num < 5

     + let num=num+1

     + let num < 5

     + let num=num+1

     + let num < 5

     + let num=num+1

     + let num < 5

     + print The grand total is 5

5    The grand total is 5


Table 12.14. Debug Commands and Options

Command

Function

How It Works

export PS4='$LINENO '

The PS4 prompt by default is a +

You can reset the prompt. In this example, a line number will be printed for each line.

Ksh –x scriptname

Invokes ksh with echo option.

Displays each line of the script after variable substitution and before execution.

Ksh –v scriptname

Invokes ksh with verbose option.

Displays each line of the script before execution, just as you typed it.

Ksh –n scriptname

Invokes ksh with noexec option.

Interprets but does not execute commands.


set –x or

set –o xtrace


Turns on echo option.

Traces execution in a script.

set +x

Turns off echo.

Turns off tracing.

trap 'print $LINENO ' DEBUG

Prints value of $LINENO for each line in the script.

For each script command, the trap action is performed. See format for trap.

Trap 'print Bad input' ERR

 

If a nonzero exit status is returned, the trap is executed.

trap 'print Exiting from $0' EXIT

 

Prints message when script or function exits.

typeset –ft

Turns on tracing.

Traces execution in a function.


EXPLANATION

  1. The Korn shell is invoked with the –x option. Echoing is turned on. Each line of the script will be displayed on the screen, followed by the result of executing that line. Variable substitution is performed. Alternatively, the –x option can be used in the script instead of at the command line (e.g., #!/bin/ksh –x).

  2. The lines are preceded by the plus (+) sign, the PS4 prompt.

  3. The while loop is entered. It will loop 4 times.

  4. The value of num is incremented by by 1.

  5. After the while loop exits, this line is printed.

Example 12.67.

(The Script)

     #!/bin/ksh

     # Scriptname: todebug2

1    trap 'print "num=$num on line $LINENO"' DEBUG

     num=1

     hile (( num < 5 ))

     do

         (( num=num+1 ))

     done

     print The grand total is $num



(The Output)

     $ todebug2

2    num=1 on line 3

     num=1 on line 4

     num=2 on line 6

     num=2 on line 4

     num=3 on line 6

     num=3 on line 4

     num=4 on line 6

     num=4 on line 4

     num=5 on line 6

     num=5 on line 4

     The grand total is 5

     num=5 on line 8

     num=5 on line 8


EXPLANATION

  1. LINENO is a special Korn shell variable that holds the number of the current script line. The DEBUG signal, used with the trap command, causes the string enclosed in single quotes to be executed every time a command in the script is executed.

  2. As the while loop executes, the value of the variable num and the line of the script are displayed.

    Previous Section  < Day Day Up >  Next Section