Lesson 3 | Tracing shell scripts |
Objective | Use shell options to test each line of a script. |
-v
and –x
.
The –x
and –v
options locate many, but not all, errors in a script
–v
option displays each line of your script before it is run. It prints the line exactly as you included it in your script–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 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.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
or –v
options for just a portion of your script. Here is more information about tracing a portion of a script
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.set x
and set v
are combined into one statement to turn on both options in the middle of the script: #!
/bin/sh 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"
if
statement.