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


Listing 22.3 Complete Listing of the addperson Script

     1  #!/bin/sh
     2  # Name: addperson
     3  # Desc: add a person addressbook
     4  # Args: -n <name>
     5  #       -e <email>
     6  #       -a <postal address>
     7  #       -p <phone number>
     8
     9  # initialize the variables
    10
    11  PATH=/bin:/usr/bin
    12  MYADDRESSBOOK=$HOME/addressbook
    13  NAME=""
    14  EMAIL=""
    15  ADDR=""
    16  PHONE=""
    17
    18  # create a function to remove the : from user input
    19 
    20  remove_colon() { echo "$@" | tr ':' ' ' ; }
    21
    22  if [ $# -lt 1 ] ; then
    23
    24      # this is interactive mode
    25
    26      # enable erasing input
    27
    28      stty erase '^?'
    29
    30      # prompt for the info
    31
    32      printf "%-10s " "Name:"    ; read NAME
    33      printf "%-10s " "Email:"   ; read EMAIL
    34      printf "%-10s " "Address:" ; read ADDR
    35      printf "%-10s " "Phone:"   ; read PHONE
    36
    37  else
    38
    39      # this is noninteractive mode
    40
    41      # initialize a variable for the usage statement
    42
    43      USAGE="'basename $0' [-n name] [-e email] [-a address]
            ⇒[-p phone]"
    44
    45      # scan the arguments to get the info
    46
    47      while getopts n:e:a:p:h OPTION
    48      do
    49          case $OPTION in
    50              n) NAME="$OPTARG" ;;
    51              e) EMAIL="$OPTARG" ;;
    52              a) ADDR="$OPTARG" ;;
    53              p) PHONE="$OPTARG" ;;
    54              \?|h) echo "USAGE: $USAGE" >&2 ; exit 1 ;;
    55          esac
    56      done
    57  fi
    58 
    59  NAME="'remove_colon $NAME'"
    60  EMAIL='remove_colon $EMAIL'"
    61  ADDR="'remove_colon $ADDR'"
    62  PHONE="'remove_colon $PHONE'"
    63
    64  echo "$NAME:$EMAIL:$ADDR:$PHONE" >> "$MYADDRESSBOOK"
    65
    66  exit $?

This script first initializes its variables (lines 11–16). You set the internal variables that store the user information to null in order to avoid conflicts with exported variables in the user’s environment.

The next step is to create the following function (line 20):

remove_colon() { echo "$@" | tr ':' ' ' ; }

You use this function to make sure that the user’s input doesn’t contain any colons.

You then check to see whether any arguments are given (line 22). If this is so, you enter interactive mode (lines 23–36); otherwise, you enter noninteractive mode (lines 38–56).

In interactive mode, you prompt for each piece of information and read it in. Before you produce the first prompt, you issue a stty command (line 28) to make sure the user can erase any mistakes made during input.

In noninteractive mode, you use getopts to obtain the information provided on the command line. In this section you also initialize the variable USAGE to contain the usage statement for this command.

After the information has been obtained, you call the remove_colon function for each variable (lines 59–62). Because the user can potentially specify information that contains colons, skipping this step could corrupt the address book and confuse the showperson script.

Finally you update the address book and exit.

An example of using the script in interactive mode is

$ ./addperson
Name:      James Kirk
Email:     jim@enterprise-a.starfleet.mil
Address:   1701 Main Street James Town Iowa UFP
Phone:

Here you provided only the name, email address, and postal address for Jim Kirk. When you look up James Kirk in the address book, you find that this field is empty:

$ ./showperson
Name:      James Kirk
Email:     jim@enterprise-a.starfleet.mil
Address:   1701 Main Street James Town Iowa UFP
Phone:

You can do the same addition using the noninteractive form:

$ ./addperson -n "James Kirk" -e jim@enterprise-a.starfleet.mil \
-a "1701 Main Street James Town Iowa UPF"

Notice that on the command line you need to quote the entries that contain spaces.

Deleting a Person

Occasionally, you need to delete a person from the address book. In this section, I will look at a script called delperson that deletes people from the address book.

Deleting a person from the address book is a harder task because you have to make sure that those people you really want to delete are deleted. The two main tasks you need to perform are

1.  Make a list of the lines in the address book that match the specified name.
2.  Based on user feedback, delete the appropriate entries from the address book.

Because the delete operation can potentially remove information from the address book, you have to be extra careful about making backups and working on copies of the original address book.

To simplify prompting and printing error messages, this script uses the shell function library libTYSP.sh that was introduced in Chapter 21.

The basic flow of the script is

1.  Make a copy of the address book and use the copy for all modifications.
2.  Get a list of all matching lines from this copy and store them in a deletion file.
3.  For each line in the deletion file, print it out formatted and ask the user whether the line should be deleted.
4.  If the user wants the line deleted, remove the line from the copy of the address book.
5.  After all the deletions are performed, make a backup of the original address book.
6.  Make the edited copy the address book.
7.  Clean up temporary files and exit.

For each of these steps, you use a function to make sure that the operations performed succeeded.

The complete delperson script is given in Listing 22.4 (the line numbers are provided for your reference).


Previous Table of Contents Next