Question

In: Computer Science

Dominion Consulting in Sydney needs a shell script (to run on the Bourne shell) to maintain...

Dominion Consulting in Sydney needs a shell script (to run on the Bourne shell) to maintain its employee records file, which contains the following information (fields) about each employee:

  • telephone number (8 digits, first digit non-zero);

  • family name (alphabetic characters);

  • first name (alphabetic characters);

  • department number (2 digits) and

  • job title (alphabetic characters).
    This script should let users add, delete, search for, and display specific employee information.

Task Requirements

  1. Create a text file named records containing the following records with fields delimited by colons (:) :

    95671660:Jones:Sarah:45:sales manager 93272658:Smith:John:43:technical manager 98781987:Williams:Nick:35:computer officer 99893878:Brown:Sarah:12:electrician 95673456:Couch:David:26:chef 95437869:Anderson:Sarah:19:CEO

  2. In the beginning of your script you need to check to see whether the required text file (records) actually exists under the current directory. If records does not exist, your script must display an error message and then exit.

  3. The script must be named menu.sh. The script must show a menu of operations (see requirement 4) that a user may choose from. Among other tasks, these operations automate the following four activities:

    1. 1) Display all current employee records to the screen;

    2. 2) Search for and display specific employee record(s) (search all fields, ignoring case);

    3. 3) Add new records to the records file; and

    4. 4) Delete records from the records file.

  4. The script must show the following menu and prompt the user to select an option:

    Dominion Consulting Employees Info Main Menu ============================================ 1 – Print All Current Records
    2 - Search for Specific Record(s)

    3 - Add New Records 4 – Delete Records Q - Quit

  5. After a user chooses an option and the selected operation has been completed, the user is prompted to press the Enter key, and then the menu must be displayed again so that the user can make another selection.

  6. When adding a new employee record (option 3), the script must ask the user to provide the following field values (in order):

    phone number family name given name department number job title

  7. Each field must be correctly validated before the next field is requested. If validation fails, the user must be asked to enter the field again (and this process repeats until the field is successfully validated). After each successful employee record is added, the user should be prompted to ask if they want to enter another employee record (this process will continue until the user indicates they do not want to add another employee record).

    Validation is as follows: family name: must be alphabetic characters and/or spaces. given name:must be alphabetic characters and/or spaces. job title: must be alphabetic characters and/or spaces. phone number: the first digit of an eight-digit phone number must not be zero. Each telephone number must be unique (not already exist) department number: A valid department number must only contain 2 digits.

  8. When deleting records (option 4), the user must be prompted to enter a valid phone number. If the phone number is invalid, then the user will continue to be prompted until they enter a valid phone number. If a record matches the phone number entered (hint: as phone numbers are unique, only one record should match), then the matching record must be displayed, and the user must be prompted to confirm they want to delete the matching record.

Solutions

Expert Solution

SOLUTION:

The following solution has been written in Bash script. It has been implemented in Cygwin Bash.

There are seperate bash functions for Search, Delete and Add operations.

The Employee records are stored in an External file "records" as specified in the question.

The User menu displays the options every time you run the script and after you perform any one of the operations specified until you choose to quit.

PROGRAM

#!/bin/bash

# Enployee Record file
employeeList="records"

# Function to Search for a particular record
search()
{
 loop=1
 until [ $loop -eq 0 ]
 do
  echo "Enter the keyword:"
  read key
  if [ -z $key ]; then
   echo "Enter a Valid Keyword to Search "
   continue
  fi
  temp="result.tmp"
  if `grep -i $key $employeeList >$temp`
  then
   cat $temp
   rm -f $temp
   echo "Press Enter to continue..."
   read
   break
  else
   echo "$key not found"
  fi
 done
}

# Function to display all records
display()
{
 awk -F: '{print $2,$3,$1,$4,$5}' $employeeList | sort
 #sort -t: +1 $employeeList | awk -F : '{print $2,$3,$1,$4,$5}' 
 echo "Press Enter to continue..."
 read
}

# Function to delete a particular record
delete()
{
 temp="file.tmp"
 loop=1
 echo "Delete Employee Record "
 until [ $loop -eq 0 ]
 do
  echo "Enter a phone number:"
  read tel
  test -z $tel
  if [ $? -eq 0 ];
   then
   echo "Enter the Phone Number"
   continue
  fi
  if `echo $tel | grep -E '^[1-9]{1}[0-9]{7}$' > /dev/null 2>&1`
   then
   break
  else
   echo "Invalid phone number"
  fi
 done
  if `grep $tel $employeeList >$temp 2>&1`
  then
    echo "The following match has been found"
    cat $temp
    rm $temp
    echo "Confirm deletion: (y)es or (n)o:"
    read option
  case $option in
  y|Y|yes|Yes)
   sed "/$tel/d" $employeeList >$temp
   rm $employeeList
   mv $temp $employeeList
   echo -e "Record has been deleted.\n"
   ;;
 
  n|N|no|No)
    return
    ;;
  *)
    ;;
    esac
  else
  echo "$tel not found"
  return
 fi 
}

# Function to Add a New Employee Record
add()
{
 loop=1
 echo "Add New Employee Record"

 # Get and Validate Phone Number
 until [ $loop -eq 0 ]
 do
  echo "Phone Number (8 digits):"
  read tel
  test -z $tel
  if [ $? -eq 0 ];
   then
   echo "Enter the Phone number"
   continue
  fi
  if `echo $tel | grep -E '^[1-9]{1}[0-9]{7}$' > /dev/null 2>&1`
   then
   break
  else
   echo "Invalid phone number"
   continue
  fi
 done

 # Get and Validate Family Name
 until [ $loop -eq 0 ]
 do
  echo "Family Name:"
  read familyname
  test -z $familyname > /dev/null 2>&1
  if [ $? -eq 0 ];
   then
   echo "Enter the Family Name"
   continue
  fi
  if `echo $familyname | grep -E '^[a-zA-Z]{1,}([ ]*|[a-zA-Z]{1,})*$' > /dev/null 2>&1`
   then
   break
  else
   echo "Family name can contain only alphabetic characters and spaces"
   continue
  fi
 done

 # Get and Validate First Name
 until [ $loop -eq 0 ]
 do
  echo "Given name:"
  read firstname
  test -z $firstname > /dev/null 2>&1
  if [ $? -eq 0 ];
   then
   echo "Enter the Name"
   continue
  fi
  if `echo $firstname | grep -E '^[a-zA-Z]{1,}([ ]*|[a-zA-Z]{1,})*$' > /dev/null 2>&1`
   then
   break
  else
   echo "Given name can contain only alphabetic characters and spaces"
   continue
  fi
 done

 # Get and Validate Department Number 
 until [ $loop -eq 0 ]
 do
  echo "Department Number:"
  read num
  test -z $num
  if [ $? -eq 0 ];
   then
   echo "Enter Department number"
   continue
  fi
  if `echo $num | grep -E '^[0-9]{2}$' > /dev/null 2>&1`
   then
   break;
  else
   echo "A valid department number contains 2 digits "
   continue
  fi
 done

# Get and Validate Job Title
 until [ $loop -eq 0 ]
 do
  echo "Job Title:"
  read job
  test -z $job >/dev/null 2>&1
  if [ $? -eq 0 ];
   then
   echo "Enter Job Title"
   continue
  fi
  if `echo $job | grep -E '^[a-zA-Z]{1,}([ ]*|[a-zA-Z]{1,})*$' > /dev/null 2>&1`
   then
   break
  else
   echo "Job title can contain only alphabetic characters and spaces"
   continue
  fi
 done
 echo "Adding new employee record to the records file … "
 echo "$tel:$familyname:$firstname:$num:$job" >> $employeeList
 echo "New record saved"
 echo "Add another? (y)es or (n)o:"
 read option
 case $option in
  y|Y|yes|Yes)
    add_record
    ;;
 n|N|no|No)
    return
    ;;
 *)
    ;;
 esac
}

 

while :
 do
 test -f $employeeList
 if  [ $? -eq 1 ]
  then
  echo "The '$employeeList' is not exist."
  exit 1
 fi
 echo "Dominion Consulting Employees Info Main Menu"
 echo "============================================"
 echo "1 - Print All Current Records"
 echo "2 - Search for Specific Record(s)"
 echo "3 - Add New Records"
 echo "4 - Delete Records"
 echo "Q  - Quit"
 echo "Your Selection:"
 read option
 case $option in
 
  1)
  display
  ;;
  2)
  search
  ;;
  3)
  add
  ;;
  4)
  delete
  ;;
  q|Q)
  exit
  ;;
  *)
  echo  "Choose a Valid Option"
  echo "Press Enter to continue..."
  read
  ;;
 esac
 done

SCREENSHOT

SAMPLE OUTPUT

Note:

For the sample run, all data given have been valid. However, if an invalid entry is provided for phone number, first name, family name, department number, job title appropriate message will be displayed and prompted for re-entry.

Hope this helps


Related Solutions

Write a shell script (to run on the Bourne shell) called cp2.sh to copy all the...
Write a shell script (to run on the Bourne shell) called cp2.sh to copy all the files from two named directories into a new third directory. Timestamps must be preserved while copying files from one directory into another. Task Requirements Three directory names must be supplied as arguments to your script when it is executed by the user. In the beginning of your script you need to check that the first two directory names provided (e.g. dir1 and dir2) exist...
Write a complete shell script that first asks the user to enter a URL. The script...
Write a complete shell script that first asks the user to enter a URL. The script should read the URL into a variable named url. The shell script should then retrieve the file associated with the URL by using the curl command. The output of the curl command should be redirected to a file named html_file. The shell script should then use the grep command to search the file named html_file for the word manhattan. Finally, the shell script should...
(10pts) Write a shell script to display your name to the screen. (10pts) Write a shell...
(10pts) Write a shell script to display your name to the screen. (10pts) Write a shell script to take a directory as an argument and display the contents of that directory (10pts) Write a shell script that takes any command as a parameter and displays help on that command (e.g. the result of the execution of man <command>). (10pts) Write a shell script that requires two file names as parameters and copies content of one file into another without asking...
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string...
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string expressed as -rwxrwxrwx and prints out whether or not theobject is a directory, file or link. The string is read in from standard input. Convert String (Part 2) Modify the script so its able to print out the octal permission which the string represents along with the file type. This is in addition to part 1. Convert String (Part 3) For the script in...
Create a shell script called (Main.sh). The script must call the following scripts (FileS.sh), (Work.sh), (Ether.sh),...
Create a shell script called (Main.sh). The script must call the following scripts (FileS.sh), (Work.sh), (Ether.sh), (Arch.sh) and (Buzz.sh). The first script Main.sh must have two subroutines. The First Subroutine will display the following messages on the screen: 1-Display User login name, date and time. 2-Display System boot time. 3-Working path and Shell type 4-Display Home directory and number of files and directories in your Home. 5-Message describing briefly the requited tasks.
Design two shell programs working on Linux (Ubuntu) Design a shell script program, 1) reading given...
Design two shell programs working on Linux (Ubuntu) Design a shell script program, 1) reading given only two integer numbers from command line arguments and computing their multiplication. If two integer numbers are not given, print “Wrong Input” on your screen. Note that, the number of arguments is known when the script runs. Take a screenshot showing your shell program and its execution step. Design a shell program to remove all the shell programming files ending with sh on your...
Examine the following shell script and describe its function line-by-line: #! /bin/bash #This script backs up...
Examine the following shell script and describe its function line-by-line: #! /bin/bash #This script backs up the Oracle DB rm -f /SAN/backup-oracle* if tar -zcvf /SAN/backup-oracle- 'date +%F'.tar.gz/oracledb/* then echo "Oracle backup completed on 'date'" >>/var/log/oraclelog else echo "Oracle backup failed on 'date'" >>/var/log/oraclelog mail -s ALERT [email protected] </var/log/oraclelog fi
this is bash scripting. a. Write a shell script that adds an extension “.deb” to all...
this is bash scripting. a. Write a shell script that adds an extension “.deb” to all the files in a directory. b. Write a command sequence or a script to insert a line “GHIJKLM” at every 10th line of a file? c. Write a bash command or script to find all the files modified in less than 5 days and print the record count of each.
Perl is a programming language that can be used on Linux. Write a Perl shell script...
Perl is a programming language that can be used on Linux. Write a Perl shell script named phone.pl that prompts the user to enter first or last or any portion of person’s name, so that can be found the appropriate entry in the phone directory file called “phones”. If the user tries to enter name as the argument on the command line, he/she will get a warning message “You need to provide name when prompted by this script!” If the...
Write a brief shell script that will take in a specific file name, prompt the user...
Write a brief shell script that will take in a specific file name, prompt the user whether they would like to gzip, bzip2, or xz compress the file. Depending on response, the script then ought to compress the provided file with the corresponding method
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT