Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

home / study / engineering / computer science / computer science questions and a

ID: 3715109 • Letter: H

Question

home / study / engineering / computer science / computer science questions and answers / The Language Is Bash Create A Bash Script Called "lookup" That Will Do The Following: A. Sort ...

Question: The language is bash Create a bash script called "lookup" that will do the following: a. Sort "da...

The language is bash

Create a bash script called "lookup" that will do the following:

a. Sort "datafile" by last names. (datafile is a text file i will create with first and last names and an address)

b. Show the user the contents of "datafile".

c. Tell the user the number of entries in the file and Ask the user if they would like to add a new entry to datafile

d. If the answer is y or yes then prompt the user to enter new name and address. Each variable will be stored separetly and you will append the new information into the datafile.

e. Sort the datafile by last names and Tell the user you added their entry and show them the line preceded by the line number.

check to see if the "datafile" exists and if it is readable and writable. Add a menu to the "lookup" script to resemble the following: [1] Add entry [2] Delete entry [3] View entry [4] Exit

Write a function to handle each of the items in the menu. After the user has selected a valid entry, and the function has completed, ask if the user would like to see the menu again. If an invalid entry is entered, the program should print: "Invalid entry, try again." and the menu will be redisplayed. You already have the Add entry part of the script written. The Add entry routine should now include code that will check to see if the name is already in the datafile and if it is, tell the user so. If the name is not there, add the new entry. Now write the code for the Delete entry, View entry, and Exit functions. Each 'function' should be contained inside a bash function which gets invoked upon the user selection. The Delete part of the script should first check to see if the entry exists before trying to remove it. If it does, notify the user; otherwise, remove the entry and tell the user you removed it. On exit, make sure that you use a digit to represent the appropriate exit status.

Explanation / Answer

#!/bin/bash

add()
{
read -p "Enter the first name: " firstName
      read -p "Enter the last name: " lastName
      read -p "Enter the address: " address
      if grep "$firstName $lastName $address" datafile > /dev/null
       then
           echo "Entry Exists."
   else
      echo "$firstName $lastName $address" >> datafile
   fi
}

delete()
{
   read -p "Enter the first name of the entry to be removed: " dFirstName
   read -p "Enter the last name of the entry to be removed: " dLastName
   read -p "Enter the address of the entry to be removed: " dAddress
   if grep "$dFirstName $dLastName $dAddress" datafile > /dev/null
   then
       sed -i "/<$dFirstName $dLastName $dAddress>/d" ./datafile
   else
       echo "Entry NOT Exists"
   fi
}

view()
{
   sort -k 3 datafile
}

while true
do
echo "1.Add a Entry"
echo "2.Delete a Entry"
echo "3.View Entry"
echo "4.Exit"
read -p "Please select any of the above options: " option
case "$option" in
   "1") add
;;
   "2") delete
;;
   "3") view
;;
   "4") exit 1
;;
    *) echo "Invalid Input. Please try again. "
;;
esac
done

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote