The following is a simple phone book script that is easy to understand and
uses a bunch of basic shell script commands and statements.
Pbook stores names and numbers in a file called dlist.
I have commented the script extensively, so it is a little messy.
Check out the uncommented version for a clearer view of the code
This script contains 2 functions and a main while
loop. Most Shell scripts I have seen do not use function, but when working with
menus they can be very useful to modularize you program. The only problem with using functions
is some old Xenix systems and maybe some old Unix OS's don't support functions.
The function must come before the call. In other words
the shell reads the file
from top to bottom, and the function must be read into
memory before it is
referenced. The two functions are called
"lookup" and "addmore".
A function consists or the function
name lookup open and close parentheses
() and open and close curly brackets { } with commands in between
the { }.
Functions are read into memory and are available
instantly when referenced so
they are good to use when you will be using the commands in them more
than once before the script is done.
#!/bin/sh # Tells the OS (Operating System) to use the Bourn Shell
lookup() # Function called lookup.
{
clear # Clears the screen as soon as the script starts.
echo "" # Echo's blank lines to screen.
echo ""
echo ""
echo " Select phone number by First or Last name."
echo " If proper spelling is not known, first several letters will match all"
echo " occurrences. (Interrupt to abort) "
echo ""
echo "Please Enter Name >>>\c" #Waits for user to enter information.
read NAME #Reads information entered, and puts
#it into NAME (a variable)
# Below: Checks the file /u/doug/bin/dlist, and if it doesn't exists
# as a regular file, echos the message to the screen
if [ ! -f /u/doug/bin/dlist ]; then
echo "You must have some phone numbers in you pbook before you can look"
echo "one up!"
sleep 2 # Pauses for to seconds so you can read the message.
clear # The screen is cleared
break # Exits the function but not the script. Control
# is returned to the while loop (main body of script)
fi
if [ -z "$NAME" ]; then # Checks to see if $NAME (a variable)
echo "You didn't enter a name!" # has a value. If not then prompt again.
echo "Please Enter Name >>>\c" # Wait for name to be entered.
read NAME # Put the value entered into
# NAME (variable)
fi
# Below: Grep searches through /u/doug/bin/dlist
# for the pattern found in the $NAME variable.
# If grep finds the pattern in $NAME it prints the line containing the
# pattern to STDOUT (screen).
# The -i option to grep tells grep to ignores the "case" of the pattern.
# In other words, if $NAME = DOUG than grep -i will match doug, Doug,
# DoUG, douG, etc ...
# 2> /dev/null is how we make any error messages that grep would
# normally display to STDERR (the screen) go away. 2> means STDERR,
# /dev/null is a file that is like a hole in the back of the computer
# that shit just runs out, never to return.
grep -i "$NAME" /u/doug/bin/dlist 2> /dev/null
# Below: A case statement checks the exit status for grep.
# Exit status is a value returned form the command when it is complete.
# This may be different form machine to machine, but grep returns a 0
# if it finds what it is looking for, 1 if it didn't find anything and 2
# if no pattern was given. $? is the shell variable that hold the exit
# status. To see the exit status on the command line type echo $? after
# a command is complete.
# A case statement is an easy way to doing a lot of checks or tests.
# In English this case statement says "In case the exit status of
# grep is 1; then display the message and return control to the
# while loop (where the function was called from)." "In case the exit status
# is 2; display the message, sleep for 2 seconds, and call the same function
# again. " If the exit status is neither then the function is done and control
# returns to the while loop.
case "$?" in
1) echo "Name not in phone listing.";;
2) echo "You didn't enter a name to search for" ;sleep 2; lookup;;
esac
} # End of function called lookup
addmore() # A function definition (name)
{
clear # Clear the screen
echo ""
echo "" # Blank lines
echo ""
echo " Enter information to be stored in phone list."
echo " (Interrupt Aborts)"
echo ">>\c" # \c means no carriage return.
# (keep the cursor on the line)
read NEWNAME # Waits for input and assignees the input value to NEWNAME
# Below: Appends the value in $NEWNAME to the file /u/doug/bin/dlist
echo "$NEWNAME" /u/doug/bin/dlist
#Below: Sorts dlist and puts the sorted list back into the same file.
sort -o /u/doug/bin/dlist /u/doug/bin/dlist
} # End of function addmore
# Below is the Start of the main body of program (the while loop)
clear # Clears the screen
while true # True is a statement that returns true (1). So by saying
do # while true it is like saying "do this until something
# happens to be false (like hitting ctrl C or delete)
echo ""
echo ""
echo " PBOOK PHONE LIST"
echo ""
echo ""
echo ""
echo " 0. exit program"
echo " 1. select phone number"
echo " 2. add name and phone number"
echo " 3. display dlist"
echo " 4. edit dlist (vi editor)"
echo ""
echo "Enter Option >>>\c"
read ANS # Waits for user to enter something
# and stores the input in $ANS
case $ANS in
0 ) clear; exit;; # If $ANS is 0 clear screen and exit
1 ) lookup;; # If $ANS is 1 call lookup
2 ) addmore; clear;; # If $ANS is 2 call addmore and clear the screen
# after addmore finishes. Of course after the
# screen clears the while loop is still true so
# it redisplays the menu and wait for input
3 ) more /u/doug/bin/dlist ;clear;; # IF $ANS is 3 pages the dlist file and
# clears the screen when done.
4 ) vi /u/doug/bin/dlist ; clear;; # Edits the file dlist with vi and
# clears the screen when done.
* ) echo "INVALID CHOICE!"; sleep 2; clear;; # Echo the error message
# sleeps 2 seconds and
# clears the screen
esac
done
Back to Doug's home page.