#!/bin/bash
# pb.sh: phone book
# Written by Rick Boivie, and used with permission.
# Modifications by ABS Guide author.
MINARGS=1 # Script needs at least one argument.
DATAFILE=./phonebook
# A data file in current working directory
#+ named "phonebook" must exist.
PROGNAME=$0
E_NOARGS=70 # No arguments error.
if [ $# -lt $MINARGS ]; then
echo "Usage: "$PROGNAME" data-to-look-up"
exit $E_NOARGS
fi
if [ $# -eq $MINARGS ]; then
grep $1 "$DATAFILE"
# 'grep' prints an error message if $DATAFILE not present.
else
( shift; "$PROGNAME" $* ) | grep $1
# Script recursively calls itself.
fi
exit 0 # Script exits here.
# Therefore, it's o.k. to put
#+ un-hashmarked comments and data after this point.
# ------------------------------------------------------------------------
Sample "phonebook" datafile:
John Doe 1555 Main St., Baltimore, MD 21228 (410) 222-3333
Mary Moe 9899 Jones Blvd., Warren, NH 03787 (603) 898-3232
Richard Roe 856 E. 7th St., New York, NY 10009 (212) 333-4567
Sam Roe 956 E. 8th St., New York, NY 10009 (212) 444-5678
Zoe Zenobia 4481 N. Baker St., San Francisco, SF 94338 (415) 501-1631
# ------------------------------------------------------------------------
$bash pb.sh Roe
Richard Roe 856 E. 7th St., New York, NY 10009 (212) 333-4567
Sam Roe 956 E. 8th St., New York, NY 10009 (212) 444-5678
$bash pb.sh Roe Sam
Sam Roe 956 E. 8th St., New York, NY 10009 (212) 444-5678
# When more than one argument is passed to this script,
#+ it prints *only* the line(s) containing all the arguments.
AskApache Web Development