10.4. Debugging Scripts
C/TC shell scripts often fail because of some simple syntax error or logic error. Options to the csh command to help you debug your programs are provided in Table 10.3.
Example 10.7.
(The -v and -x Options)
1 % cat practice
#!/bin/csh
echo Hello $LOGNAME
echo The date is `date`
echo Your home shell is $SHELL
echo Good-bye $LOGNAME
2 % csh -v practice
echo Hello $LOGNAME
Hello ellie
echo The date is `date`
The date is Sun May 23 12:24:07 PDT 2004
echo Your login shell is $SHELL
Your login shell is /bin/csh
echo Good-bye $LOGNAME
Good-bye ellie
3 % csh -x practice
echo Hello ellie
Hello ellie
echo The date is `date`
date
The date is Sun May 23 12:24:15 PDT 2004
echo Your login shell is /bin/csh
Your login shell is /bin/csh
echo Good-bye ellie
Good-bye ellie
Table 10.3. echo (–x) and verbose (–v)Option | What It Does |
---|
As Options to csh and tcsh |
csh –x scriptname
tcsh -x scriptname
| Display each line of script after variable substitution and before execution. |
csh –v scriptname
tcsh -v scriptname
| Display each line of script before execution, just as you typed it. |
csh –n scriptname
tcsh -n scriptname
| Interpret but do not execute commands. | As Arguments to the set Command | set echo | Display each line of script after variable substitution and before execution. | set verbose | Display each line of script before execution, just as you typed it. | As the First Line in a Script |
#!/bin/csh –xv
#!/bin/tcsh -xv
| Turns on both echo and verbose. These options can be invoked separately or combined with other csh invocation arguments. |
EXPLANATION
The contents of the C shell script are displayed. Variable and command substitution lines are included so that you can see how echo and verbose differ. The –v option to the csh command causes the verbose feature to be enabled. Each line of the script is displayed as it was typed in the script, and then the line is executed. The –x option to the csh command enables echoing. Each line of the script is displayed after variable and command substitution are performed, and then the line is executed. Because this feature lets you examine what is being replaced as a result of command and variable substitution, it is used more often than the verbose option.
Example 10.8.
(Echo and Verbose)
1 % cat practice
#!/bin/csh
echo Hello $LOGNAME
echo The date is `date`
set echo
echo Your home shell is $SHELL
unset echo
echo Good-bye $LOGNAME
% chmod +x practice
2 % practice
Hello ellie
The date is Sun May 26 12:25:16 PDT 2004
--> echo Your login shell is /bin/csh
--> Your login shell is /bin/csh
--> unset echo
Good-bye ellie
EXPLANATION
The echo option is set and unset within the script. This enables you to debug certain sections of your script where you have run into a bottleneck, rather than echoing each line of the entire script. The ––> marks where the echoing was turned on. Each line is printed after variable and command substitution and then executed.
Example 10.9.
1 % cat practice
#!/bin/csh
echo Hello $LOGNAME
echo The date is `date`
set verbose
echo Your home shell is $SHELL
unset verbose
echo Good-bye $LOGNAME
2 % practice
Hello ellie
The date is Sun May 23 12:30:09 PDT 2004
--> echo Your login shell is $SHELL
--> Your login shell is /bin/csh
--> unset verbose
Good-bye ellie
EXPLANATION
The verbose option is set and unset within the script. The ––> marks where verbose was turned on. The lines are printed just as they were typed in the script and then executed.
|