Write a detail note on debugging tools
in unix.
Whenever a script is written it
must be tested to find out
whether the script is behaving
as desired or not. Often
multiple tests are necessary .
Sometimes the tests do not yield
desired results. In such cases it
is necessary to debug the
script. Shell scripts can be
debugged either with the
execute trace option (-x) or
verbose option (-v). With these
Options one can check the
value of all the variables
involved and view the logical
flow of the Program.
Debug option can be used in
two ways. In one method the
Options are used in the
command line during the
execution of the script . For
Example, in order to debug a
Script called mesg.sh the
command line will be $sh-x.
mesg.sh GOOD LUCK, where
GOOD LUCK is the argument
to the script. In the other
Method, the set -x statement is
Used as the very first
statement within the script
Itself, as shown in the
following examples. The effects
Of set -x is undone using the
set +x statement as the last
Statement within the script.
Of course, after successful
Debugging, both these set
statements must be edited.
The execute trace option
(-x) prints each command,
preceded by a plus (+) sign,
before it is executed . It is
also replaces the value of
each variable accessed in
the statement. For example
in the statement x=$y, the
$y is replaced by its actual
Value. Thus, if the actual
value of $y is 25, the
statement x=25 will be
displayed. Similarly
expressions value and test
values are also displayed.
However, expression that
appears on the right, hand
Side of an assignment
statement appears first with
the variables subtstitude with
theire values and preceded
by ++ characters.
Given below are the
contents of the mesg.sh file
displayed using the cat
command as well as the
trace of the execution of
the same message GOOD
LUCK as its argument.
$cat mesg.sh
# To print message for
required number of times.
Set -x "how many numbers
Of times the message is to
be displayed? "
read count
Untill [ $count -eq 0]
do
echo $*
count =`expr $count -1`
done
set +x
$
$sh mesg.sh GOOD LUCK
+echo `how many numbers
Of times the message is to be
Displayed? `
How many numbers Of times
the message is to be
displayed ?
+read count
3
+ `[ 3 -eq 0 `]`
+echo GOOD LUCK
GOOD LUCK
++ expr 3- 1
+ count =2
+`[`2 -eq 0 `]`
+ echo GOOD LUCK
GOOD LUCK
++expr 2-1
+count=1
+`[ 1 -eq 0`]`
+echo GOOD LUCK
GOOD LUCK
++ expr 1-1
+ count=0
+`[`0 -eq 0`]`
$
The use of the verbose
Option -v almost similarly.
However it prints
every statement of the before
the substitution for the
variables is made .
If necessary both the -x and
-v options can be used
together.
0 Comments