OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Tracing

Bugs are often cases of unexpected logical flow within a script. That is, portions of the script are either never being executed, or are being executed in the wrong order or at the wrong time. To view the actual flow of the program, we use a technique called tracing.

One tracing method involves placing informative messages in a script that display the lo- cation of execution. We can add messages to our code fragment:



echo "preparing to delete files" >&2

if [[ -d $dir_name ]]; then if cd $dir_name; then

echo "deleting files" >&2

rm *

else

echo "cannot cd to '$dir_name'" >&2 exit 1

fi else

echo "no such directory: '$dir_name'" >&2 exit 1

fi

echo "file deletion complete" >&2

echo "preparing to delete files" >&2

if [[ -d $dir_name ]]; then if cd $dir_name; then

echo "deleting files" >&2

rm *

else

echo "cannot cd to '$dir_name'" >&2 exit 1

fi else

echo "no such directory: '$dir_name'" >&2 exit 1

fi

echo "file deletion complete" >&2


We send the messages to standard error to separate them from normal output. We also do not indent the lines containing the messages, so it is easier to find when it’s time to re- move them.

Now when the script is executed, it’s possible to see that the file deletion has been per- formed:



[me@linuxbox ~]$ deletion-script preparing to delete files deleting files

file deletion complete [me@linuxbox ~]$

[me@linuxbox ~]$ deletion-script preparing to delete files deleting files

file deletion complete [me@linuxbox ~]$


bash also provides a method of tracing, implemented by the -x option and the set command with the -x option. Using our earlier trouble script, we can activate tracing for the entire script by adding the -x option to the first line:


#!/bin/bash -x

#!/bin/bash -x

Debugging


# trouble: script to demonstrate common errors number=1

if [ $number = 1 ]; then

echo "Number is equal to 1." else

echo "Number is not equal to 1."

fi

# trouble: script to demonstrate common errors number=1

if [ $number = 1 ]; then

echo "Number is equal to 1." else

echo "Number is not equal to 1."

fi


When executed, the results look like this:



[me@linuxbox ~]$ trouble

+ number=1

+ '[' 1 = 1 ']'

+ echo 'Number is equal to 1.' Number is equal to 1.

[me@linuxbox ~]$ trouble

+ number=1

+ '[' 1 = 1 ']'

+ echo 'Number is equal to 1.' Number is equal to 1.


With tracing enabled, we see the commands performed with expansions applied. The leading plus signs indicate the display of the trace to distinguish them from lines of regu- lar output. The plus sign is the default character for trace output. It is contained in the PS4 (prompt string 4) shell variable. The contents of this variable can be adjusted to make the prompt more useful. Here, we modify the contents of the variable to include the current line number in the script where the trace is performed. Note that single quotes are required to prevent expansion until the prompt is actually used:



[me@linuxbox ~]$ export PS4='$LINENO + '

[me@linuxbox ~]$ trouble

5 + number=1

7 + '[' 1 = 1 ']'

8 + echo 'Number is equal to 1.' Number is equal to 1.

[me@linuxbox ~]$ export PS4='$LINENO + '

[me@linuxbox ~]$ trouble

5 + number=1

7 + '[' 1 = 1 ']'

8 + echo 'Number is equal to 1.' Number is equal to 1.


To perform a trace on a selected portion of a script, rather than the entire script, we can use the set command with the -x option:


#!/bin/bash

# trouble: script to demonstrate common errors number=1

#!/bin/bash

# trouble: script to demonstrate common errors number=1



set -x # Turn on tracing

if [ $number = 1 ]; then

echo "Number is equal to 1." else

echo "Number is not equal to 1."

fi

set +x # Turn off tracing


set -x # Turn on tracing

if [ $number = 1 ]; then

echo "Number is equal to 1." else

echo "Number is not equal to 1."

fi

set +x # Turn off tracing


We use the set command with the -x option to activate tracing and the +x option to de- activate tracing. This technique can be used to examine multiple portions of a trouble- some script.


Top OS Cloud Computing at OnWorks: