Debugging Shell Scripts


If you write shell scripts of any complexity at all, sooner or later bugs are going to creep in. Although there is no full-fledged debugger for the shell, it does provide some primitive debugging capabilities in the form of being able to trace each action that is performed. You turn on tracing by adding -xv to the end of the #!/bin/sh line in your script, so it looks like this:

#!/bin/sh -xv


The x switch means for the shell to print every command, preceded by a plus sign (+), to STDERR. The v switch puts the shell in verbose mode. This has the effect of causing the script to write out everything it does to the error filehandle so that you can debug more easily.

This works best if you pipe the output to more or less so that it doesn't scroll off the screen faster than you can read itand also if you redirect both STDOUT and STDERR to the same place so that you can see both the output of the script and the errors. Listing 10.23 shows a simple example; enter it into a file called xvtest and set the file executable:

Listing 10.23. A Simple Demonstration of Tracing

#!/bin/sh -xv # Demonstrate the use of tracing in shell script debugging result=`echo "2 * 12 / (2 + 2)" | bc` echo $result exit 0

To execute this program so that you will see both STDOUT and STDERR and pipe the output to more, use the following command to invoke the program:

# ./xvtest 2>&1 | more


Tip

Piping the output to more (or less, which is the same thing) is only useful when the script is non-interactivewhen it requires no user intervention. Another technique, one that lets you debug interactive scripts, is to pipe the output instead into the tee program to redirect STDOUT and STDERR into a file, which you can then read in another terminal with less (press shift + F while in less to have it automatically show new data arriving at the end of the file).


The program will produce the following output:

1.  #!/bin/sh -xv 2.  # Demonstrate the use of tracing in shell script debugging 3.  result=`echo "2 * 12 / (2 + 3)" | bc` 4.  + echo 2 * 12 / (2 + 3) 5.  + bc 6.  + result=4 7.  echo $result 8.  + echo 4 9.  4 10. exit 0 11. + exit 0


You can now see everything that this program did. The lines with plus signs (+) in front of them are the results of actions in the program. For example, line 3 in the preceding example assigns the results of a calculation to the variable result. Lines 4, 5, and 6 show the actions that were taken. Line 4 shows the echo command, line 5 shows the execution of bc, and line 6 shows the variable assignment, where 4 is assigned to the variable result.

This example also shows how variable expansion works. Notice lines 8, 9, and 10. In line 8, the echo statement is read. In line 9, the variable is expanded so that the echo statement becomes echo 4. Then, in line 10, the actual output of the echo statement is produced.




FreeBSD 6 Unleashed
FreeBSD 6 Unleashed
ISBN: 0672328755
EAN: 2147483647
Year: 2006
Pages: 355
Authors: Brian Tiemann

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net