QuestionQuestion

Implement in shell a "telephone book" program, which works according to the description given below (lines starting with "--" are comments, user inputs are in bold face, programs responses are in lower case. In the actual execution, user commands can be in lower/upper case). Note: do not use tools such as sed or awk, only standard shell.

$ book
~/misc/book doesn't exist, do you want to create one (y/n)? y

response on the same line. If ~/misc/book already exists, the execution starts from the menu as given below:

~/misc/book created -- or nothing if it already exists

There are 5 users on the system -- or 3, or 1, etc

Available commands:
Look someone up
Add someone to the phone book
Remove someone from the phone book
List all names and numbers in the phone book
Change the prompt
See the menu
Exit the program
Please select one of the above (1-6): 2

Type in your entry, terminate with a single RETURN on the line:

>> STEVEN BARK

>> 778 MAIN STREET

>> WOLFVILLE

>> 542-2201

Steven Bark added to the book

: 5 -- Menu is not shown, ":" is a prompt

Type in your new prompt: Yes my dear :

Yes my dear: 5

Type in your new prompt: -- just RETURN, to use default prompt

: 8

? -- book doesn't know it

: 2

Type in your entry, terminate with a single RETURN on the line:

>> STEVEN LONG

>> 118 UNKNOWN STREE T

>> KENTVILLE

>> 678-2201

: 6

Would you like to:
Look someone up
Add someone to the phone book
Remove someone from the phone book
List all names and numbers in the phone book
Change the prompt
See the menu
Exit the program
Please select one of the above (1-6): 1

Enter name to look up: LONG

There is 1 item(s):

#1:

STEVEN LONG
118 UNKNOWN STREET
KENTVILLE
678-2201

: 1

Enter name to look up: 2201

There is 2 item(s):

#1:

STEVEN LONG
118 UNKNOWN STREET
KENTVILLE
678-2201

#2:

STEVEN BARK
778 MAIN STREET
WOLFVILLE
542-2201

: 3

Enter name to remove: 542

There is 1 item(s):

STEVEN BARK
778 MAIN STREET
WOLFVILLE
542-2201

Do you want to remove it? (y/n): Y

: 7

$

Hand in the source and the script of the execution.

Hint. The shell procedure shown below displays the number of lines in the file whose name is passed on the command line

a=0
while real line
do
a=`expr $a + 1`
done < $1
echo "Final line count is: $a"

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

#!/bin/bash

filename="./book"
menu="Available commands:\nLook someone up\nAdd someone to the phone book\nRemove someone from the phone book\nList all names and numbers in the phone book\nChange the prompt\nSee the menu\nExit the program"

CWD=$(pwd)
prompt=":"

if [ -f "$filename" ]
then
echo "";
else
while true; do
echo "$CWD/book does not exist"
read -p "Would you like to create it(y/n): " yn
case $yn in
[Yy]* ) touch book; break;;
[Nn]* ) ;;
* ) echo "Please answer yes or no.";;
esac
done
fi

echo -e $menu
read -p "Please select one of the above(1-6): " choice

while true; do
case $choice in
[1]*)
count=1
read -p "Enter name to look up: " search
while read line
do
name=$line
read line
address=$line
read line
town=$line
read line
number=$line
if [[ $address =~ $search ]] || [[ $name =~ $search ]] || [[ $town =~ $search ]] || [[ $number =~ $search ]] ; then
echo "#$count:"
echo "$name"
echo "$address"
echo "$town"
echo -e "$number\n"
((count++));
fi
done < $filename;;...
$50.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Computer Science - Other Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats