11.4. Debugging Scripts

TC shell scripts often fail due to some simple syntax error or logic error. Options to the tcsh command are provided to help you debug your programs. See Table 11.3.

Table 11.3. echo (-x) and verbose (-v)
As options to tcsh
tcsh -x scriptname Display each line of script after variable substitution and before execution.
tcsh -v scriptname Display each line of script before execution, just as you typed it.
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/tcsh -xv Turn on both echo and verbose. These options can be invoked separately or combined with other csh invocation arguments.

Example 11.8.
(The -v and -x Options)
1 > cat practice
					#!/bin/tcsh -f
  echo Hello $LOGNAME
  echo The date is `date`
  echo Your home shell is $SHELL
  echo Good-bye $LOGNAME
  
2 > tcsh -v practice
					echo Hello $LOGNAME
  Hello ellie
  echo The date is `date`
  The date is Mon May 24 12:26:07 PDT  2000
  echo Your login shell is $SHELL
  Your login shell is /bin/csh
  echo Good-bye $LOGNAME
  Good-bye ellie
  
3 > tcsh -x practice
					echo Hello ellie
  Hello ellie
  echo The date is `date`
  date
  The date is Mon May 24 12:26:15 PDT 2000
  echo Your login shell is /bin/tcsh
  Your login shell is /bin/tcsh
  echo Good-bye ellie
  Good-bye ellie
				

Explanation

  1. The contents of the TC shell script are displayed. Variable and command substitution lines are included so that you can see how echo and verbose differ.

  2. The -v option to the tcsh 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.

  3. The -x option to the tcsh 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 allows you to examine what is being replaced as a result of command and variable substitution, it is used more often than the verbose option.

Example 11.9.
(Echo and Verbose)
1 > cat practice
					#!/bin/tcsh -f
  echo Hello $LOGNAME
  echo The date is `date`
					set echo
					echo Your home shell is $SHELL
					unset echo
					echo Good-bye $LOGNAME
  
2 > chmod +x practice
3 > practice
					Hello ellie
  The date is Mon May 24 12:25:16 PDT 2000
  -->echo Your login shell is /bin/tcsh
  -->Your login shell is /bin/tcsh
  -->unset echo
     Good-bye ellie
				

Explanation

  1. 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.

  2. The execute permission is turned on with chmod.

  3. The --> marks where the echoing was turned on. Each line is printed after variable and command substitution and then executed.

Example 11.10.
1 > cat practice
					#!/bin/tcsh -f
  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 Mon May 24 12:30:09 PDT 2000
-->echo Your login shell is $SHELL
-->Your login shell is /bin/csh
-->unset verbose
     Good-bye ellie
				

Explanation

  1. The verbose option is set and unset within the script.

  2. The --> marks where verbose was turned on. The lines are printed just as they were typed in the script and then executed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.117.212