In: Computer Science
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
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
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.
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) Display all current employee records to the screen;
2) Search for and display specific employee record(s) (search all fields, ignoring case);
3) Add new records to the records file; and
4) Delete records from the records file.
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
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.
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
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.
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.
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