#! /bin/sh
# To display Menu information
#
function displayMenu(){
echo ""
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 ""
}
# Ask users whether to continue
#
function ifCon(){
echo -e "Press Enter to continue...\c"
read ifContinue
if [ "$ifContinue" = "" ]
then
continue;
fi
}
# Ask users to get phone number when add new employee records
#
function addPhone(){
while true
do
echo -e "Phone Number (xxxxxxxx): \c"
read phoneNum
# Check if the phone number is not null, 8 digits, first digit nonzero and only number
# and does not exist
#
if [ "$phoneNum" = "" ]
then
echo "Phone Number not entered"
continue
elif [[ $phoneNum = 0* ]] || [ ! `expr length $phoneNum` -eq 8 ] || [[ $phoneNum =~ [^0-9] ]]
then
echo "Invalid phone number"
continue
elif grep $phoneNum records>/dev/null
then
echo "Phone number exists"
continue
else
break
fi
done
echo ""
}
# Ask users to get Name or Job when add new employee records
# When use:
# addNameOrJob "Family Name" | "Given Name" | "Job Title"
#
function addNameOrJob(){
while true
do
echo -e "$1: \c"
read inputName
# Get user's input, which can contain only alphabetic characters and spaces
#
if [[ $inputName =~ [^a-zA-Z' '] ]]
then
echo "$1 can contain only alphabetic characters and spaces"
continue
else
# Recording the second parameters, different instructions will be used
#
if [ "$1" = "Given Name" ]
then
givenName=$inputName
elif [ "$1" = "Family Name" ]
then
familyName=$inputName
elif [ "$1" = "Job Title" ]
then
jobTitle=$inputName
fi
break
fi
done
echo ""
}
# Used to add new records, using values assigned before
#
function addNewEmployee(){
echo "Adding new employee record to the records file ..."
#Ex. 95673456:Couch:David:26:chef
#
newEmployee=$phoneNum":"$familyName":"$givenName":"$departNum":"$jobTitle
if echo $newEmployee >> records
then
echo "New record saved"
fi
echo ""
}
# Used to add department information of new employee records
#
function addDepartment(){
while true
do
echo -e "Department Number: \c"
read departNum
# department number should be 2 digits
#
if [[ $departNum = [0-9][0-9] ]]
then
break
else
echo "A valid department number contains 2 digits"
continue
fi
done
echo ""
}
# Ask users if delete the certain record
#
function deleteRecord(){
grep $deletePhNum records
echo ""
echo -e "Confirm deletion: (y)es or (n)o: \c"
read ifDeletion
echo ""
case "$ifDeletion" in
y|Y)
# Delete certain record
#
grep -v $deletePhNum records > tran
cat tran > records
rm tran
echo "Record deleted."
break ;;
n|N) break ;;
*) echo "Invalid input"; continue ;;
esac
}
# Check if the "records" file exits, if not, then say error message
#
if [ ! -f records ]
then
echo "Error: There is no 'Records' file in the directory :("
echo ""
exit
fi
while true
do
# First, display the main Menu which can let users to select special operations
# like add, delete, search for or display
#
displayMenu
echo -e "Your Selection: \c"
read menus
#
#
case "$menus" in
1)
cat records
echo -e "\n"
ifCon
;;
2)
# Keyword search
#
echo -e "Enter keyword: \c"
read keyword
if [ "$keyword" = "" ]
then
echo "Keyword not entered"
elif grep $keyword records>/dev/null
then
grep $keyword records
else
echo "$keyword not found"
fi
ifCon
;;
3)
# Use some predefined function to finish the "Add" operation
#
while true
do
echo "Add New Employee Record"
addPhone
addNameOrJob "Family Name"
addNameOrJob "Given Name"
addDepartment
addNameOrJob "Job Title"
addNewEmployee
echo -e "Add another? (y)es or (n)o: \c"
read conti
case "$conti" in
y|Y) continue ;;
n|N) break ;;
*) echo "Invalid input"; continue ;;
esac
done
;;
4)
# Delete employee record
#
echo "Delete Employee Record"
echo ""
while true
do
echo -e "Enter a phone number: \c"
read deletePhNum
# Phone number should not be empty, length less than 8 or not exists
#
if [ "$deletePhNum" = "" ]
then
echo "Phone Number not entered"
continue
elif [[ $deletePhNum = 0* ]] || [ ! `expr length $deletePhNum` -eq 8 ]
then
echo "Invalid phone number"
continue
elif ! grep $deletePhNum records>/dev/null
then
echo "Phone number not found"
continue
else
deleteRecord
fi
done
;;
q|Q)
echo "Bye"
exit
;;
*)
echo "Invalid input"
;;
esac
done