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


After you have dealt with the source directory, you examine the destination directory, which requires special treatment because it can mean two different things:

  If the destination directory exists (line 40), the user wants the source directory moved into the destination directory.
  If the destination directory does not exist (line 44), the user wants the source directory moved and renamed in the parent directory of the specified destination directory.

In the first case you need to determine the absolute path to the destination directory (line 42). In the second case you need to obtain the following information:

  The name of the destination directory’s parent directory. This value is stored in DESTDIR (line 49).
  The new name the user wants for the source directory. This value is stored in NEWNAME (line 50).

After you convert the value of DESTDIR to an absolute path, you check to make sure that it exists. If it doesn’t, the script reports an error to the user and exits.

In the next few lines (lines 68–79), you try to move the files from the source to the destination using the values you determined for the source and destination directories. If something goes wrong during this process, you report an error and exit.

After a copy has been successfully made using tar, you check to see whether a rename has been requested (line 83). If it has been, you try to change the name of the copy from its original name to the new name (line 94). If this process fails, the script issues an error and exits.

Finally, if all the other operations are successful, you delete the original directory (line 110). Any error in this operation is reported.

Examples

Now look at two examples of using this script to move a directory between file systems.

In the first example, you want to move a source directory into a destination directory on a different file system:

$ ls /tmp
ch22           ps_data        sdtdbcache_:0
$ ./mvdir.sh /tmp/ch22 /home/ranga/docs/book
$ ls /tmp /home/ranga/docs/book
/tmp:
ps_data        sdtdbcache_:0
/home/ranga/docs/book
ch20     ch21     ch22     ch23

As you can see, the directory ch22 was moved from /tmp to the directory

/home/ranga/docs/book.

In the second example, you move the same directory, but you also rename it:

$ ls /home/ranga/docs/book
ch20     ch21     ch22     ch23
$ ./mvdir /home/ranga/docs/book/ch22 /tmp/ch22-work
$ ls /home/ranga/docs/book /tmp
/home/ranga/docs/book:
ch20     ch21     ch23
/tmp:
ch22-work     ps_data     sdtdbcache_:0

Here the directory was moved and renamed.

Maintaining an Address Book

I often get business cards or email messages from people I need to keep in touch with. Sometimes I lose these email messages or business cards, leading to problems when I need to contact someone important.

A nice solution to this problem would be to store all the contact information on my computer so that I could access and manipulate it easily. In this section, I will look at developing a set of scripts that work together to maintain a simple address book.

The address book will store the following information:

  Name
  Email address
  Postal address
  Phone number

Each of these pieces of information can contain almost any character including spaces or other special characters such as the dash (-), period, (.), or single quote (). Thus you need to hold the information in a format that allows for these characters. A commonly used format is to separate each piece of information using the colon (:) character. For example, the following information:

Sriranga Veeraraghavan
ranga@soda.berkeley.edu
1136 Wunderlich Dr. San Jose CA 95129
408-444-4444

can be stored as:

Sriranga Veeraraghavan:ranga@soda.berkeley.edu:1136 Wunderlich Dr.
⇒San Jose CA 95129:408-444-4444

Here any special character, except the colon, can be used. Also this format enables you to make any field optional. For example,

:vathsa@kanchi.bosland.us::408-444-4444

could indicate that only the email address and phone number were known for a particular person.

To maintain your address book, you need a few scripts:

  showperson to show information about one or more people in the address book
  addperson to add a person to the address book
  delperson to delete a person from the address book

The following scripts assume that the address book is stored in the file $HOME/addressbook.

Showing People

One of the main tasks any address book must perform is looking up a person. You will develop a script called showperson to accomplish this.

To find information about a person, you can use grep command. For example,

$ grep vathsa addressbook

lists all the lines that contain the word vathsa in the file addressbook. For your address book, the output might look like the following:

:vathsa@kanchi.bosland.us::408-444-4444

As you imagine, your showperson script should format the results of the grep command. A nice format would be to list the name, email address, postal address, and phone number on separate lines. You can do this using an awk command:

awk -F: '{ printf "Name: %s\nEmail: %s\nAddress: %s\nPhone:
⇒%s\n\n",$1,$2,$3,$4 ; }'


Previous Table of Contents Next