home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Unix Power ToolsUnix Power ToolsSearch this book

Chapter 37. Shell Script Debugging and Gotchas

37.1. Tips for Debugging Shell Scripts

Depending on the Bourne shell version you have, the error messages it gives can be downright useless. For instance, it might say just End of file unexpected. Here are a few tricks to use to get a little more information about what's going on. Remember, it's probably best for you to use one of shells derived from the Bourne shell, rather than the C shell, for scripting.

37.1.1. Use -xv

Start your script like this:

#!/bin/sh -xv

(If your Unix can't handle #!, use the command set -xv (Section 35.25)). The -xv shows you what's happening as the shell reads your script. The lines of the script will be shown as the shell reads them. The shell shows each command it executes with a plus sign (+) before the command.

Note that the shell reads an entire loop (for, while, etc.) before it executes any commands in the loop.

If you want to run a script with debugging but you don't want to edit the script file, you can also start the shell explicitly from the command line and give the options there:

% sh -xv scrfile

Debugging output is usually pretty long, more than a screenful, so I pipe it to a pager like less. But the shell sends its debugging output to stderr, so I pipe both stdout and stderr (Section 43.4) to the pager.

$ scrfile 2>&1 | less

Do you want to save the debugging output in a file and see it on your screen, too? Use tee (Section 43.8) to copy the scrfile stdout and stderr; add tee to the pipeline before the pager.

$ scrfile | tee outfile 2>&1 | less

If the script is slow, you can run it in the background. Redirect the shell's output and errors (Section 43.5, Section 27.11) into a temporary file. Use tail -f (Section 12.10) to "watch" the log file. If you want to do something else while the script runs, just kill the tail command (with CTRL-c or your interrupt key), do something else, then start another tail -f when you want to watch again.

Finally, if the script normally writes something to its standard output, you can split the normal and debugging outputs into two files (Section 43.1).

Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.