Computing.Net > Forums > Unix > creating a phonebook

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

creating a phonebook

Reply to Message Icon

Name: Nicholas liu
Date: February 2, 2002 at 21:49:59 Pacific
Comment:

hi ...i am presenting creating a phone book..i go the menu running already but i dont know how to link the subprogrames to the menu....is it like this?

echo" 1) display a telephone number"
echo" 2) add a new number"
read selection
case $selection in
"1")
echo"u wan ot dispaly a number"
displaynumber----> my file to display a number..
"2")
echo "u wan to to add a new number"
addnewNmber
esac...

pls correct if i am wrong..this is onli part of my script....asap..thanx!!



Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: February 4, 2002 at 08:59:30 Pacific
Reply:

#!/bin/sh

display_number() {
echo 'Enter number: \c'
read number
if [ "$number" ] ; then
grep "$number" myphonebook
fi
}

echo 'Enter selection: \c'
read selection

case $selection in
1) echo "u wan ot dispaly a number"
display_number ;;
(more case statement)
esac

The above code shows subroutine display_number and shows it being called from a case statement. Each section of case statement must be terminated with a double semicolon. Getting a selection from the user and the case statement to process it needs to be in a looping structure such as a while statement.

Some statements will produce an error if a variable is null. For example:

if [ $number = 999 ]

would get evaluated by the shell as:

if [ = 999 ]

when $number is null, which of course it could be if entered by a user. To guard against this, enclose in double-quotes:

if [ "$number" ]

My subroutine is just doing a grep because I do not know the format of myphonebook. A simple grep will not suffice because that will search entire line for the string, and it might find other occurrences that you do not want, such as in someone's address. When you know the exact format of your phone book, you can code a more discriminating statement.


0
Reply to Message Icon

Related Posts

See More


SCO disk cloning No Shell



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: creating a phonebook

creating a UNIX phonebook www.computing.net/answers/unix/creating-a-unix-phonebook/2652.html

A simple script for creating a link www.computing.net/answers/unix/a-simple-script-for-creating-a-link/6981.html

creating a report www.computing.net/answers/unix/creating-a-report/2561.html