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.4 Complete Listing of the delperson Script

     1  #!/bin/sh
     2  # Name: delperson
     3  # Desc: del a person addressbook
     4  # Args: $1 -> name of person to delete
     5
     6  # get the helper functions
     7
     8  . $HOME/lib/sh/libTYSP.sh
     9
    10  PATH=/bin:/usr/bin
    11
    12  # check that a name is given
    13
    14  if [ $# -lt 1 ] ; then
    15      printUSAGE "'basename $0' name"
    16      exit 1
    17  fi
    18
    19  # check that the address book exists
    20
    21  MYADDRESSBOOK="$HOME/addressbook"
    22  if [ ! -f "$MYADDRESSBOOK" ] ; then
    23      printERROR "$MYADDESSBOOK does not exists, or is
            ⇒not a file."
    24      exit 1
    25  fi
    26 
    27  # initialize the variables holding the location of the
    28  # temporary files
    29  TMPF1=/tmp/apupdate.$$
    30  TMPF2=/tmp/abdelets.$$
    31
    32  # function to clean up temporary files
    33
    34  doCleanUp() { rm "$TMPF1" "$TMPF1.new" "$TMPF2" 2>
        ⇒/dev/null ; }
    35
    36  # function to exit if update failed
    37  Failed() {
    38      if [ "$1" -ne 0 ] ; then
    39          shift
    40          printERROR $@
    41          doCleanUp
    42          exit 1
    43      fi
    44  }
    45
    46  # make a copy of the address book for updating,
    47  # proceed only if sucessful
    48
    49  cp "$MYADDRESSBOOK" "$TMPF1" 2> /dev/null
    50  Failed $? "Could not make a backup of the address book."
    51
    52  # get a list of all matching lines from the address book copy
    53  # continue if one or more matches were found
    54 
    55  grep "$1" "$TMPF1" > "$TMPF2" 2> /dev/null
    56  Failed $? "No matches found."
    57
    58  # prompt the user for each entry that was found
    59
    60  exec 5< "$TMPF2"
    61  while read LINE <&5
    62  do
    63
    64      # display each line formatted
    65
    66      echo "$LINE" | awk -F: '{
    67          printf "%-10s %s\n%-10s %s\n%-10s %s\n%-10s %s\n\n",\
    68                 "Name:",$1,"Email:",$2,"Address:",$3,
                       ⇒"Phone:",$4 ;
    69      }'
    70
    71      # prompt for each line, if yes try to remove the line
    72
    73      promptYESNO "Delete this entry" "n"
    74      if [ "$YESNO" = "y" ] ; then
    75
    76          # try to remove the line, store the updated version
    77          # in a new file
    78
    79          grep -v "$LINE" "$TMPF1" > "$TMPF1.new" 2> /dev/null
    80          Failed $? "Unable to update the address book"
    81
    82          # replace the old version with the updated version
    83
    84          mv "$TMPF1.new" "$TMPF1" 2> /dev/null
    85          Failed $? "Unable to update the address book"
    86 
    87      fi
    88  done
    89  exec 5<&-
    90
    91  # save the original version
    92
    93  mv "$MYADDRESSBOOK" "$MYADDRESSBOOK".bak 2> /dev/null
    94  Failed $? "Unable to update the address book"
    95
    96  # replace the original with the edited version
    97
    98  mv "$TMPF1" "$MYADDRESSBOOK" 2> /dev/null
    99  Failed $? "Unable to update the address book"
   100
   101  # clean up
   102
   103  doCleanUp
   104
   105  exit $?

In the first part of the script (lines 8–30), you perform some initialization. Specifically, you perform the following actions:

1.  Retrieve the helper functions from libTYSP.sh (line 8).
2.  Check to make sure a name to delete is given (lines 14–17).
3.  Check to make sure that the address book exits (lines 21–25).
4.  Initialize the variables for the temporary files (lines 29 and 30) and the PATH (line 10).

After initialization, you create a few additional helper functions:

  doCleanUp, to remove the temporary files (line 34)
  Failed, to issue an error message, remove the temporary files and exit if a critical command fails (lines 37–44)

The first main step in the script is to make a copy of the address book (line 49). If this step fails, you exit (line 50). If this step is successful, you make a list of all the lines in the address book that match the name specified by the user (line 55). If you cannot successfully make this file, you exit (line 56).

Next you enter the delete loop (lines 60–89). For each line that matches the name provided by the user you print a formatted version of the line (lines 66–69). Notice that you are using the same awk statement from the showperson script.

For each matching line, you ask the user whether the entry should be deleted (line 73). If the user agrees (line 74), you do the following:

1.  Try to delete the line from the copy of the address book. Store the modified version in a different file (line 79).
2.  Replace the copy of the address book with the modified copy (line 84).

If either of these operations fail, you exit (lines 80 and 85).

After the deletes are finished, you make a backup of the original address book (line 93). Then you replace the address book with the fully edited version (line 98). Again you exit if either operation fails (lines 94 and 99).

Finally you clean up and exit.

Here is an example of this script in action:

$ ./delperson Sriranga
Name:      Sriranga Veeraraghavan
Email:     ranga@soda.berkeley.edu
Address:   1136 Wunderlich Dr. San Jose CA
Phone:     408-444-4444

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

Here I replied yes to the question. You can confirm that the delete worked as follows:

$ ./showperson Sriranga
$

Because there is no output from showperson, this entry has been deleted.


Previous Table of Contents Next