Here's a useful little shell script that we've used at O'Reilly &
Associates. If you run it as
phone
, it gives you peoples' phone
numbers - it searches files named
phone
in your home directory
and in a system location. If you run it as
address
, it does the
same thing for files named
address
. Lines from the system file
are labeled
sys>
; lines from your personal file are marked
pers>
. For example:
%
phone tom
pers>Tom VW's mother, Barbara Van Winkel in Vermont 802-842-1212
pers>Tom Christiansen [5/10/92] 201/555-1212
sys>Flitecom (Dave Stevens, Tom Maddy) (301) 588-1212
The script uses
egrep
(
27.5
)
to search the file; the
egrep -i
option means you can type
tom
and the script will find lines with either
Tom
or
tom
(or
TOM
or...). The two names for this script are
both
links (
18.3
)
to the same file. Of course, you can adapt the script for things
besides phone numbers and addresses.
test
touch
$#
|
#!/bin/sh
# LINK BOTH THE phone AND address SCRIPTS TOGETHER; BOTH USE THIS FILE!
myname="`basename $0`" # NAME OF THIS SCRIPT (USUALLY address OR phone)
case "$myname" in
phone|address)
sysfile=/work/ora/$myname # SYSTEM FILE
persfile=${HOME?}/$myname # PERSONAL FILE
;;
*) echo "$0: HELP! I don't know how to run myself." 1>&2; exit 1 ;;
esac
if test ! -f $persfile
then touch $persfile
fi
case $# in
0) echo "Usage: $myname searchfor [...searchfor]
(You didn't tell me what you want to search for.)" 1>&2
exit 1
;;
*) # BUILD egrep EXPRESSION LIKE (arg1|arg2|...) FROM NAME(S) USER TYPES:
for arg
do
case "$expr" in
"") expr="($arg" ;;
*) expr="$expr|$arg" ;;
esac
done
expr="$expr)"
esac
# SEARCH WITH egrep, USE sed TO ADD sys> TO START OF FILENAMES FROM
# SYSTEM FILE AND pers> TO START OF FILENAMES FROM HOME LIST:
egrep -i "$expr" $persfile $sysfile |
sed -e "s@^$sysfile:@sys>@" -e "s@^$persfile:@pers>@"
exit
|
The comments in the script explain what each part does.
The most interesting part is probably the
for
loop (
44.16
)
and
case
statement (
44.5
)
that build the
egrep
expression.
For instance, if you type the command
phone tom mary
,
the script builds and runs an
egrep
command as if you'd typed this:
%
egrep -i "(tom|mary)" /u/me/phone /work/ora/phone
/u/me/phone:Tom VW's mother, Barbara Van Winkel in Vermont 802-842-1212
/u/me/phone:Tom Christiansen [5/10/92] 201/555-1212
/work/ora/phone:Flitecom (Dave Stevens, Tom Maddy) (301) 588-1212
...
The
sed
(
34.24
)
command turns the pathnames from
egrep
into
pers>
and
sys>
.
You can install this script from the CD-ROM or you can just
type it in. If you type in the script, put it in an executable file
named
phone
. (If all users on your system will share it, your
system administrator should put the script in a central directory such
as
/usr/local/bin
.) Then make a link to it:
%
chmod 755 phone
%
ln phone address