Tải bản đầy đủ (.pdf) (11 trang)

Shell Script Debugging

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (91.25 KB, 11 trang )

■ ■ ■
PART 1
Basic Scripting
Techniques
3
■ ■ ■
CHAPTER 1
Shell Script Debugging
E
ven though this book isn’t a “how to script” manual, some concepts that are funda-
mental to writing successful scripts should be discussed. Debugging is one of them.
Debugging code is a significant part of writing code. No matter how disciplined you are
or how skilled you become at coding, you will have bugs in your code, in the form of either
syntax or logic errors. The syntactical problems tend to be simpler to resolve since many
times they show up when the code throws an error when it is run. The logical bugs, on the
other hand, may be more difficult to track down since the code may run without error, but
the resulting output does not match the design of the program. The more complex your
code becomes as your skill increases, the more difficult these types of problems will be to
detect.
Since writing bug-free code is nearly impossible, you need a few techniques up your
sleeve that will help you finish, diagnose, repair, and clean up your code. This chapter pre-
sents a few ways to debug code that I have used consistently and that help me extract
details from the inner workings of my scripts. These techniques validate that the code is
living up to my expectations and demonstrate where the code needs more work to per-
form the intended task.
Shell Trace Options
The first technique—using the set command—is the simplest to implement and can give
you great amounts of detail about how the logic is progressing and the values of variables
internal to your script. Using the set command is really just using shell options to display
verbose output when the script is running. One of the functions of the set command is to
turn on and off the various options that are available in the shell. In this case, the option


being set is -x, or xtrace. This is where the running script will, in addition to any normal
output, display the expanded commands and variables of a given line of code before the
code is run. With this increased output, you can easily view what is happening in the run-
ning script and possibly determine where your problem lies.
When you put the instruction set -x into your script, each of the commands that exe-
cute after that set instruction will be displayed, together with any arguments that were
supplied to the command, including variables and their values. Each line of output will be
4
CHAPTER 1

SHELL SCRIPT DEBUGGING
preceded by a plus-sign (+) prompt to designate it as part of the trace output. Traced
commands from the running shell that are being executed in a subshell are denoted by
a double plus sign (++).
To demonstrate what the use of set -x can do for you, consider this script:
#!/bin/sh
#set -x
echo -n "Can you write device drivers? "
read answer
answer=`echo $answer | tr [a-z] [A-Z]`
if [ $answer = Y ]
then
echo "Wow, you must be very skilled"
else
echo "Neither can I, I'm just an example shell script"
fi
Note that the set -x line is currently commented out. When this script is entered in the
file example and run, the behavior is as expected.
$ ./example
Can you write device drivers? y

Wow, you must be very skilled
or
$ ./example
Can you write device drivers? n
Neither can I, Im just an example shell script
This is the output when the set -x line is uncommented:
$ ./example
+ echo -n 'Can you write device drivers? '
Can you write device drivers? + read answer
y
++ tr '[a-z]' '[A-Z]'
++ echo y
+ answer=Y
+ '[' Y = Y ']'
+ echo Wow, you must be very skilled
Wow, you must be very skilled
or
CHAPTER 1

SHELL SCRIPT DEBUGGING
5
$ ./example
+ echo -n 'Can you write device drivers? '
Can you write device drivers? + read answer
n
++ echo n
++ tr '[a-z]' '[A-Z]'
+ answer=N
+ '[' N = Y ']'
+ echo Neither can I, Im just an example shell script

Neither can I, Im just an example shell script
The output is a verbose trace of the script’s execution. Note that the lines without the
plus sign are the output of the script that would be displayed if the script were run without
tracing enabled. As you can see, this type of trace is highly useful in determining the value
that variables contain during the execution of a script, as well as the route that the code
took based on the conditions satisfied.
A shell option that is a slight variation of this output can also be used for troubleshoot-
ing. The -v option to the shell enables verbose mode and outputs the script code (as it
is being executed) to the standard error file handle (often abbreviated as stderr). More
specifically, in the case of a shell script, each line of code that is encountered during exe-
cution is output to stderr along with any other output from the script. (Chapter 9 contains
more discussion of file handles.) The following is the output from the same script when
the set -v line is implemented:
$ ./example
echo -n "Can you write device drivers? "
Can you write device drivers? read answer
y
answer=`echo $answer | tr [a-z] [A-Z]`
echo $answer | tr [a-z] [A-Z]if [ $answer = Y ]
then
echo "Wow, you must be very skilled"
else
echo "Neither can I; I'm just an example shell script"
fi
Wow, you must be very skilled
or
6
CHAPTER 1

SHELL SCRIPT DEBUGGING

$ ./example
echo -n "Can you write device drivers? "
Can you write device drivers? read answer
n
answer=`echo $answer | tr [a-z] [A-Z]`
echo $answer | tr [a-z] [A-Z]if [ $answer = Y ]
then
echo "Wow, you must be very skilled"
else
echo "Neither can I; I'm just an example shell script"
fi
Neither can I; I'm just an example shell script
The verbose (-v) option to the shell is more useful if you simply want to see the running
code of the script that you’re working with (as opposed to the expanded values of vari-
ables) to make sure the code is working as designed with the xtrace (-x) option. Both
options can be employed together by using set -xv, and you’ll see both types of output at
the same time, although it may be difficult to wade through.
Both the verbose and xtrace options are valuable in their own way for troubleshooting
both logical and syntactical problems. As with all options to the shell, they can be turned
on and off. The syntax for disabling an option is the opposite of that for turning on an
option. Instead of using a minus (-) sign as you did before to enable an option such as in
-x, you would use a plus sign, as in +x to disable the option. This will disable the option
from that point on. This is very useful if you want to debug only a small portion of the
script. You would enable the option just prior to the problem area of code, and disable it
just after the problem area so you aren’t inundated with irrelevant output.
Simple Output Statements
The next debugging technique—the use of echo or print commands in the code—is also
very simple, but it is used frequently to gather specific variable values from a running
script rather than displaying potentially large amounts of data using the set -x option.
Typically these commands are used for simple output of a script to some type of display

or file. In this case, however, they will be used as a checkpoint in the code to validate vari-
able assignments.
These additional output instructions are used regularly in at least a couple of ways. The
first way is to output the value of a specific variable at a specific time. Sometimes variables
get changed when you aren’t expecting them to be, and adding a simple output line will
show this. The main advantage of this type of output compared to set -x is that you have
the ability to format your output for ease of reading. While set -x has a valid use and is

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×