Question

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 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;;...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Related Homework Solutions

C++ Questions
Homework Solution
$30.00
Programming
C++
Palindromes
Strings
Reverse Functions
Time Formats
Parameters
Outputs
Console
Input
Output
Display
Letters
Characters
Lines
Statements
Variables
Counters
R Programming Problems
Homework Solution
$18.00
Statistics
Probability
Computer Science
R Programming
Data Frames
Libraries
Columns
Classification
Genes
Biological Data Analysis
Leukemia
Properties
Sorting Algorithms Using Programming Language C
Homework Solution
$63.00
Computer Science
Programming
C
Heap
Recursion
Quick Sort
Bubble Sort
Functions
Algorithms
Modification
Partition
Arrays
Loops
Statements
Variables
Pointers
Experiments
Data Sets
Random Numbers
Gauss-Jordan Equation Method in C++
Homework Solution
$175.00
Mathematics
Computer Science
C++
Gauss-Jordan
Linear Equations
Matrices
Input Files
Output Files
Rows
Columns
Loops
Variables
Statements
Algorithm
Results
Advanced Biostatistics Questions
Homework Solution
$50.00
Mathematics
Statistics
Biology
Study Design
Tables
Analysis
Validation
Models
Plots
Random Effects
Populations
Success
Data Files
Functions
Cloud Computing (2000 words)
Homework Solution
$58.00
Cloud
Computing
Service
Model
Delivery
IaaS
PaaS
SaaS
Software
Infrastructure
Platform
NIST
Challenge
Distributed
Environment
Get help from a qualified tutor
Live Chats