how do i write this in shell script, ubuntu linux Basic Directory For this lab y
ID: 3918309 • Letter: H
Question
how do i write this in shell script, ubuntu linux
Basic Directory
For this lab you will write a basic contacts directory. Each contact will be saved in a text file named: username_DAT where username is the last name of the contact. The system will print a menu with the following options:
New Contact.
Search Contact
Delete Contact
For option 1, the script will ask a user their first and last name, then the system will do a search for the file named lastName.dat.
If the file is found the system will print "Contact already in file" and exit.
If the file is not found the system will ask the user for the following information:
Phone Number
Address
Then it will save the information in a file named lastName,dat and exit.
For Option 2, the system will ask for a last name and use that value to search the file system for a file named lastName.dat.
If the file is found the system will print the contents of the file
If the file is not found the system will print "Contact not found" and exit.
For Option 3, the system will ask for a last name and use that value to search the file system for a file named lastName.dat
If the file is found, the system will delete the file and exit.
If the file is not found the system will print "Contact not found" and exit.
Explanation / Answer
#!/bin/bash
echo "Welcome to Contact Directory System"
echo "Please Enter number for Below Options to Select:"
echo "1. New Contact"
echo "2. Search Contact"
echo "3. Delete Contact"
read option
case $option in
1)
echo "Creating new Contact"
echo "Please Enter First Name"
read fstName
echo "Please Enter Last Name"
read lstName
ext="_DAT"
filename=$lstName$ext.dat
echo $filename
if [ -f "$filename" ]
then
echo "File Already Exists"
else
touch $filename
echo "Please Enter Phone Number"
read phone
echo "Please Enter Email Id"
read email
echo "Please Enter Address"
read add
echo "First Name : $fstName
Last Name : $lstName
Phone : $phone
Email : $email
Address : $add" > $filename
fi
;;
2)
echo "Searching Contact"
echo "Please Enter Last Name"
read lstName
ext="_DAT"
filename=$lstName$ext.dat
if [ -f "$filename" ]
then
cat $filename
else
echo "Contact Does Not Exists"
fi
;;
3)
echo "Deleting Contact"
echo "Please Enter Last Name"
read lstName
ext="_DAT"
filename=$lstName$ext.dat
if [ -f "$filename" ]
then
rm $filename
echo "$filename is Deleted"
else
echo "Contact Does Not Exists"
fi
;;
esac
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.