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:
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:
After you convert the value of DESTDIR to an absolute path, you check to make sure that it exists. If it doesnt, the script reports an error to the user and exits.
In the next few lines (lines 6879), 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.
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.
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:
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:
The following scripts assume that the address book is stored in the file $HOME/addressbook.
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 |