Tips for Debugging Shell Scripts
Bourne Shell Debugger Shows a Shell Variable
Stop Syntax Errors in Numeric Tests
Stop Syntax Errors in String Tests
Quoting and Command-Line Parameters
How Unix Keeps Time
Copy What You Do with script
Cleaning script Files
Making an Arbitrary-Size File for Testing
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.
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).
If the shell says End of file unexpected, look for a line in your script that has an opening quote but no closing quote. The shell is probably searching for but never finding the matching quote. Missing parentheses and braces ({}) can cause the same error.
If you're getting an End of file unexpected error, put these two lines near the middle of the script:
echo "DEBUG: quitting early..." 1>&2 exit
Then run your script. Those lines will print a message and stop the shell where you put them. If you don't get the End of file unexpected error anymore, you know that the problem is somewhere after the exit line, and you can move those two lines farther down and try again. Otherwise, move them up.
A message like line 23: ;; unexpected means that you have an unmatched piece of code somewhere before line 23. You'll also see fi unexpected. Look at all nested if and case statements, and statements like them, to be sure that they end in the right places.
The shell may give you an error that mentions "line 1" or another line number that seems way too small, when there's no error close to the top of your script. Look at any loops or other structures with redirected inputs or outputs (Section 43.6). Some Bourne shells start a separate shell to run these loops and lose track of the line numbers.
--JP and SJC
Copyright © 2003 O'Reilly & Associates. All rights reserved.