Previous | Table of Contents | Next |
In this book you have looked at scripts that are quite short. Thus, the issue of debugging them has boiled down to looking at their output and making sure it is correct.
For larger shell scripts, especially the kind that change system configurations, trying to deduce the source of a problem from a scripts output is insufficient. Often, by the time you get the output, it is too latethe script will have made incorrect modifications or changes. Another common scenario is adding features to a large script that someone else developed. In such cases you need to make sure your changes dont affect the rest of the script. Fortunately, the shell provides several built-in commands for enabling different modes of debugging support.
In this chapter you learn how to enable debugging, and then you will look at how to use the following debugging modes:
By now, you are quite familiar with the basic syntax for executing a shell script:
$ script arg1 arg2 argN
Here script is the name of the script, and arg1 through argN are the arguments to the script. A frequently used alternative method to execute a shell script is
$ /bin/sh script arg1 arg2 argN
Here you explicitly specify the shell, in this case /bin/sh, that you used to execute the script. The advantage of this method is that you can enable a debugging mode by supplying arguments to the shell.
Using this method of script invocation, the basic syntax for enabling a debugging mode is
$ /bin/sh option script arg1 arg2 argN
Here option is one of the debugging options covered in Table 20.1.
Option | Description |
---|---|
-n | Reads all commands, but does not execute them. |
-v | Displays all lines as they are read. |
-x | Displays all commands and their arguments as they execute. This option is often referred to as the shell tracing option. |
A third way of enabling debugging is to change the first line of the script. Usually, the first line of a script is
#!/bin/sh
UNIX uses this line to determine the shell you can use to execute a script. This indicates that the shell /bin/sh should be used to execute the script. Modify this line to specify a debugging option as follows:
#!/bin/sh option
Here option is one of the debugging options listed in Table 20.1.
Because the previously mentioned methods for enabling debugging modes take effect when a script is invoked, they are sometimes referred to as invocation activated debugging modes.
In one of the invocation activated debugging modes, the default behavior is for that debugging mode to take effect at the first line of your script and remain in effect until the last line. Sometimes you just need to debug a particular function or section of your script. In these cases, enabling debugging for the entire script is overkill.
As you see later in this chapter, the debugging output is quite extensive, and it is often hard to sort out the real errors from the noise. Address this problem with the set command to enable the debugging modes.
By using the set command, you can enable and disable debugging at any point in your shell script.
Enabling Debugging Using set
The basic syntax follows:
set option
Here option is one of the options given in Table 20.1.
You can use the set command anywhere in your shell script, and many scripts use it to change the debugging flags as part of the normal execution of the script. Because these debugging modes are activated only when the shell script programmer uses the set command, they are sometimes referred to as programmer activated modes.
Consider the following excerpt from a shell script (the line numbers are provided for your reference):
1 #!/bin/sh 2 set -x 3 if [ -z "$1" ] ; then 4 echo "ERROR: Insufficient Args." 5 exit 1 6 fi
Here the shell programmer is requesting that shell tracing (the -x option) be activated with the command from line 2:
set -x
Because this command occurs before the if statement (lines 3 through 6), shell tracing will be active while the if statement executes. Unless it is explicitly disabled later in the script, shell tracing remains in effect until the script exits. You will learn about the effect that shell tracing has on the output of a script in the Shell Tracing section of this chapter.
Disabling Debugging Using set
In addition to enabling debugging modes, you can use the set command to disable debugging modes as follows:
set +option
Here option is the letter corresponding to one of the options given in Table 20.1. For example, the command
$ set +x
disables the shell tracing debugging mode.
All the debugging modes that are enabled for a script can be deactivated using the following command:
$ set -
Enabling Debugging for a Single Function
One of the most common uses of the set command is to enable a particular debugging mode before a function executes and then disable debugging when the function finishes.
For example, if you have a function called BuggyFunction() and you only want to enable the shell tracing debugging mode while that function executes, use the following command:
set -x ; BuggyFunction; set +x ;
Here the debugging mode is enabled just before the function is called and is disabled when the function completes. This method is favored over explicitly using the set command inside a function to enable debugging because it enables the implementation of the function to remain unchanged.
Previous | Table of Contents | Next |