Unix Concepts   «Prev 

Sample history Substitutions


History Substitutions
History Substitutions

  1. history command plus output: The history command displays your recent commands. This list is helpful when you want to make history substitutions.
  2. !3 command plus output: The !3 tells the shell to re-execute your third command, or ls -F in this case. Note that commands are displayed before they rerun.
  3. !d command plus output: The !d tells the shell to re-execute the most recent command beginning with ‘d’. In this case, the date command is repeated.
  4. !! command plus output: The !! tells the shell to re-execute the previous command, which is the date command in this case.


How to Debug Shell Scripts

When writing a long and complex script, it is natural to make some mistakes, and it is difficult to look for some mistakes in a long and complex script that cannot function properly. The Bourne shell provides debugging tools to solve this problem. The sh command with some options can be used to do so. The syntax and function of the sh command for debugging scripts in the Bourne shell are as follows.
$ sh [option] scriptname

Function: to debug a shell script with the name of scriptname. Common options:
  1. -n: to read and check the syntax error of the commands in the script, but not to execute it;
  2. -v: (verbose) to display each line of the script as it is written in the script file and its execution result;
  3. -x: (echo) to display each line of the script with its arguments after variable substitution and its execution result.

To explain the functions of the three options: -n, -v, and -x, we will give one example for each of them as follows.
$ cat -n dpdebug1
1 # dpdebug1 is a script, which prompts the user for a capital letter.
2 # If user enters a letter between A and Z, it displays
a message.
3 # Write an error intentionally on the "then" line.
4 echo "Enter a capital letter (A -- Z): \c"
5 read in1
6 if [ "$in1" = [A-Z] ]
7 # then # This line is written in this way intentionally.
8 echo "Your entering is correct!"
9 fi
10 exit 0
$ sh -n dpdebug1
dpdebug1: syntax error at line 9 'fi' unexpected
$
The cat command with -n option displays each line with its line number. At line 7, then is commented out intentionally, which results in the syntax error. The sh command with -n option shows the syntax error. Correct it by
deleting the # sign, and then the dpdebug1 script can work properly.
$ cat dpdebug2
echo "This is the $0 script."
echo "The total number of arguments of this script is $#."
echo "The first argument is $1."
exit 0
$ sh -v dpdebug2 this is testing
echo "This is the $0 script."
This is the dpdebug2 script.
echo "The total number of arguments of this script is $#."
The total number of arguments of this script is 3.
echo "The first argument is $1."
The first argument is this.
exit 0
$