If you write shell scripts of any complexity at all, sooner or later bugs are going to creep in.
You turn on the tracing by adding -xv to the end of the #!/bin/sh in your script, so it looks like this:
#!/bin/sh -xv
This works best if you pipe the output to more or less, and 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. Here is a simple example:
#!/bin/sh -xv
# Demonstrate the use of tracing in shell debugging
result=`echo "2 * 12 / (2 + 2)" | bc`
echo $result
exit 0
To execute tis program so that you will see both STDOUT and STDERR and pipe the output to more, use the following command:
./xvtest 2>&1 | more
The program will produce the following output:
1. ./xvtest 2>&1 | more
2. #!/bin/sh -xv
3. # Demonstrate the use of tracing in shell debugging
4. result=`echo "2 * 12 / (2 + 2)" | bc`
5. + echo 2 * 12 / (2 + 2)
6. + bc
7. + result=4
8. echo $result
9. + echo 4
10. 4
11. exit 0
12. + exit 0
You can now see everything that this program did. The lines with + signs in front of them are results of actions in the program. For example, line 3 in the previous sample 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 finally, line 6 shows the variable assignment where 4 is assigned to the variable "result."
This sample 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.
|
|