Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Summary

In this chapter I covered using shell scripts to solve two problems:

  Moving directories
  Maintaining an address book

In the first example, I showed you how to move a directory between file systems using the tar command. This example also showed you how to use the basename and dirname commands to extract parts of a path for your use.

In the second example, you developed three scripts that you used to modify and view the contents of an address book. Some of the highlights of these scripts are:

  The showperson script showed you how the grep and awk commands can be used to format input.
  The addperson script showed you how a single script can be used in both interactive and noninteractive modes.
  The delperson script showed you how to use the grep command and file descriptors to update a file accurately.

The examples in this chapter demonstrate how you can apply the tools that you have covered in previous chapters to solve real problems. Using these scripts as examples, you can see some of the techniques used to solve everyday problems.

In the next chapter I will show you how to make sure the scripts you write are portable between different versions of UNIX.

Questions

1.  How might you simplify the following portion of the mvdir script? Specifically, how could you rewrite the main if statement, such that the else clause was unnecessary?
    40  if [ -d "$2" ] ; then
    41
    42      DESTDIR='( cd "$2" ; pwd ;)'
    43
    44  else
    45
    46      # if the destination doesn't exist then
    47      # assume the destination is the new name
    48      # for the directory
    49      DESTDIR="'/usr/bin/dirname $2'"
    50      NEWNAME="'/bin/basename $2'"
    51
    52      # if dirname returns a relative dir we will
    53      # be confused after cd'ing later on. So
    54      # reset it to the full path.
    55      DESTDIR='(cd $DESTDIR ; pwd ;)'
    56
    57      # if the parent of the destination doesn't
    58      # exist, we're in trouble. Tell the user
    59      # and exit.
    60      if [ ! -d "$DESTDIR" ] ; then
    61          printERROR "A parent of the destination
                ⇒directory $2 does not exist"
    62      fi
    63
    64  fi
    65
2.  The showperson script lists all matching entries in the address book based on a name provided by the user. The matches produced are case sensitive. How can you change the matches so they aren’t case sensitive?
3.  Both the showperson and delperson scripts reproduce exactly the following pieces of code
PATH=/bin:/usr/bin
# check that a name is given
if [ $# -lt 1 ] ; then
    printUSAGE "'basename $0' name"
    exit 1
fi
# check that the address book exists
MYADDRESSBOOK="$HOME/addressbook"
if [ ! -f "$MYADDRESSBOOK" ] ; then
    printERROR "$MYADDESSBOOK does not exists, or is
    ⇒not a file."
    exit 1
fi

and
awk -F: '{
        printf "%-10s %s\n%-10s %s\n%-10s %s\n%-10s
        ⇒%s\n\n",\
               "Name:",$1,"Email:",$2,"Address:",$3,
               ⇒"Phone:",$4 ;
    }'

How might you rewrite these script fragments so that they can be shared between these scripts instead of being replicated in both?
4.  The delperson script uses the grep command to generate a list of matching entries. This might confuse the user in the following instance:
$ ./delperson.01 to
Name:      James T. Kirk
Email:     jim@enterprise.mil
Address:   1701 Main Street Anytown Iowa
Phone:     555-555-5555

Delete this entry (y/n)? [n]

Here the to in Anytown was matched.
What changes should be made to the delperson script so that only those entries whose names match the user-specified name are selected for deletion?
(HINT: Use the sed command instead of grep).
5.  If the delperson script gets a signal while it is processing deletes, all the intermediate files are left behind. What can be done to prevent this?

Terms

File System
A file system is used by UNIX to store files and directories. Usually a file system corresponds to a hard drive or hard drive partition.
Tar File
A tape archive file created by the tar command. A tar file can contain both files and directories, making it similar to a zip file.


Previous Table of Contents Next