Unix Commands  «Prev  Next»
Lesson 3Tracing shell scripts
ObjectiveUse shell options to test each line of a script.

Tracing Shell Scripts

To locate problems in your script, the shell provides two options: -v and –x. The –x and –v options locate many, but not all, errors in a script

Errors on the #! line of a script

Both options display the lines of your script before they are run, but use different techniques. Looking at the instructions as they are run is called tracing the script.

–v option

The –v option displays each line of your script before it is run. It prints the line exactly as you included it in your script
Running the script with the –v option shows you which option your script is running. The –v option helps when your script is producing no output and appears to be in an infinite loop. You must press CTRL + C to stop this sort of script. ]

–x option

The –x option displays each line with all variables translated into their values. This is helpful when your script uses variables. To debug a problem, you must know which values your script is using in its commands.
The code in the Slide Show below contains an error. The wrong variable name is used in the sort command. This produces no error message because it is not a syntax error . This is the kind of error that the –x option helps you locate. Look through the FlipBook to see how the –x option locates this error.

–x option locates this error.

  1. Here is the error3 script. The variable called column in the read statement is mistakenly called col in the sort command.
  2. We will sort the volleylist file, which uses a colon separator, on field 6. This information is given to the error3 script, which turns this info into a sort command.
  3. The output shows the file sorted on the first column (the player’s name) instead of column 6.
  4. The script is run again with the –x option. It’s waiting for the user to enter the file name, volleylist.
  5. The user types in volleylist, then 6 for the column, then : for the separator. You can see the tracing option print out each read command before the value is read from the screen.
  6. The tracing option displays the sort command before it is run. You can see the + on it’s own without the column argument. There should be a 6 here. This tells you there is something wrong with the $col variable in the sort command.
You may also use the –x or –v options for just a portion of your script. Here is more information about tracing a portion of a script

Tracing Script Portion

You may feel confident about part of your script but uncertain about another section. In this case, you would trace only the portion of your code that you suspect is causing a problem. You can turn on the tracing feature inside your script with the set command. Using set v inside your script prints each line before it is run. Use set +v to turn off this feature. You can do the same thing with set x and set +x to show lines of code with their variables translated.
In the following script set x and set v are combined into one statement to turn on both options in the middle of the script: #!
echo "Please enter name of new user: " 
read filename 

set xv sort $filename | cat n > tempfile cp tempfile $filename set +xv
echo "
Your file is now sorted"

Tracing Shell Scripts - Exercise

Click the Exercise link below to practice tracing a shell script. Tracing Shell Scripts - Exercise
The next lesson examines errors commonly seen when using the shell’s if statement.