< Day Day Up > |
12.11. DebuggingBy 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
EXPLANATION
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
|
< Day Day Up > |