Previous | Table of Contents | Next |
By putting these commands together, you construct the showperson script given in Listing 22.2 (the line numbers are provided for your reference).
Listing 22.2 Listing of the showperson Script
1 #!/bin/sh 2 # Name: showperson 3 # Desc: show matching records in addressbook 4 # Args: $1 -> string to look for in addressbook 5 6 PATH=/bin:/usr/bin 7 8 # check that a string is given 9 10 if [ $# -lt 1 ] ; then 11 echo "USAGE: 'basename $0' name" 12 exit 1 13 fi 14 15 # check that the address book exists 16 17 MYADDRESSBOOK="$HOME/addressbook" 18 if [ ! -f "$MYADDRESSBOOK" ] ; then 19 echo "ERROR: $MYADDESSBOOK does not exist, or is not a ⇒file." >&2 20 exit 1 21 fi 22 23 # get all matches and format them 24 25 grep "$1" "$MYADDRESSBOOK" | 26 awk -F: '{ 27 printf "%-10s %s\n%-10s %s\n%-10s %s\n%-10s %s\n\n",\ 28 "Name:",$1,"Email:",$2,"Address:",$3, ⇒"Phone:",$4 ; 29 }' 30 31 exit $?
There are three main parts to the script:
In the first part (lines 1013) you check to see whether at least one argument is given. If so, the script continues; otherwise, it prints a usage message and exits.
In the second part, you check to see whether the address book exits. If it does not, the script prints an error and then exits; otherwise, it continues.
In the last part of the script, you use grep to obtain a list of matches and awk to format this list. To ensure even spacing of the output, the awk command uses formatting for both the information and its description. As an example,
$ ./showperson ranga
produces output similar to the following:
Name: Sriranga Veeraraghavan Email: ranga@soda.berkeley.edu Address: 1136 Wunderlich Dr. San Jose CA Phone: 408-444-4444
Notice how all the information in the second column is correctly aligned.
You can also use showperson to look for matches of a particular string. For example,
$ ./showperson va
produces two matches:
Name: Sriranga Veeraraghavan Email: ranga@soda.berkeley.edu Address: 1136 Wunderlich Dr. San Jose CA Phone: 408-444-4444 Name: N/A Email: vathsa@bosland.us Address: N/A Phone: 408-444-4444
One of the most important things about any address book is the capability to add information to it easily. If you need to edit the address book manually to add information, youre bound to make errors such as forgetting to add a colon to separate fields. By using a script, you can avoid such errors.
In this section I will look at a script, addperson, that enables you to add entries into the address book in two ways:
The script enters interactive mode when no options are given. If the noninteractive mode is being used, it tries to obtain information from the command line options.
In both modes you put the user-provided information into the following variables:
In interactive mode, you can prompt for the information in each record as follows:
printf "%-10s " "Name:" ; read NAME printf "%-10s " "Email:" ; read EMAIL printf "%-10s " "Address:" ; read ADDR printf "%-10s " "Phone:" ; read PHONE
After each prompt, you read and store the users input, including spaces and special characters inside the appropriate variable.
In noninteractive mode, you can use getopts to scan the options:
while getopts n:e:a:p: OPTION do case $OPTION in n) NAME="$OPTARG" ;; e) EMAIL="$OPTARG" ;; a) ADDR="$OPTARG" ;; p) PHONE="$OPTARG" ;; \?) echo "USAGE: $USAGE" >&2 ; exit 1 ;; esac done
As you can see, the options understood by the script in noninteractive mode are
After you have obtained the required information, you can update the file by appending a formatted record to the end of the addressbook file as follows:
echo "$NAME:$EMAIL:$ADDR:$PHONE" >> "$MYADDRESSBOOK"
Here you are assuming that the variable MYADDRESSBOOK contains the pathname to the address book file.
The complete addperson script is given in Listing 22.3 (the line numbers are provided for your reference).
Previous | Table of Contents | Next |