##Sorry for my bad English
Hello I am writing a script which ask for employee to add records at option 3 in a sequence: Phone number, Family name, given name, department number, job title etc.. My question is When I enter the correct phone number the script does not ask me family name or anything else and exits.. I want this code to ask for my family name, given name..... after validating correct phone number and so on.
the code is given below
#!/bin/bash
# Define the menu list here
while :
do
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 ""
read -p "Your selection: " option
# What to do if any of the above is selected.
case $option in
1)
cat records
;;
2)
read -p "Enter keyword: " keyword
if grep -i $keyword records >> .rec
then
cat .rec
elif [ -z $keyword ]
then
echo "Keyword not entered"
else
echo $keyword not found
fi
;;
3)
printf "\nAdd New Employee Record"
while :
do
read -p "Phone number (xxxxxxxx): " PhoneNumber
# Validate phone number
if [ -z "$PhoneNumber" ]
then
echo "Phone number not entered"
elif ! [ "$PhoneNumber" -eq "$PhoneNumber" ] 2> /dev/null
then
echo "Sorry integers only"
elif [ ! ${#PhoneNumber} -eq 8 ] || [[ ! ${PhoneNumber:0:1} == "9" ]]
then
echo "Invalid Phone number"
elif grep -q $PhoneNumber records
then
echo "Phone number exists"
else
echo $PhoneNumber >> records
fi
done
while :
do
read -p "Family Name: " FamilyName
# Validate Family Name
if [ -z $FamilyName ] || echo "$FamilyName" | grep -i -q '^[a-z/ ]*$'
then
echo $FamilyName >> records
else
echo "Family name can contain only alphabetic characters and spaces"
fi
done
while :
do
read -p "Given Name: " GivenName
# Validate Given Name
if [ -z $GivenName ] || echo "$GivenName" | grep -i -q '^[a-z/ ]*$'
then
echo $GivenName >> records
else
echo "Given name can contain only alphabetic characters and spaces"
fi
done
while :
do
read -p "Department Number: " DptNum
# Validate Department Number
if [ -z $DptNum ] || [ ! ${#DptNum} -eq 2 ] || ! [ "$DptNum" -eq "$DptNum" ] 2> /dev/null
then
echo "A valid department number contains 2 digits"
else
echo $DptNum >> records
fi
done
while :
do
read -p "Job Title: " JobTitle
# Validate Job Title
if [ -z $JobTitle ] || echo "$JobTitle" | grep -i -q '^[a-z/ ]*$'
then
echo $JobTitle >> records
else
echo "Job title can contain only alphabetic characters and spaces"
fi
done
printf "Adding new employee record to the records file ...\nNew record saved.\n"
while true
do
read -p "Add another? (y)es or (n)o: " choice
case $choice in
[Yy] ) echo ok; break;;
[Nn] ) exit;;
* ) echo "Please press y or n";;
esac
done
;;
4)
echo "Delete Employee Record"; echo /n
read -p "Enter a Phone number: " fon
# Validate phone number
if [ -z "$fon" ] || ! [ "$fon" -eq "$fon" ] 2> /dev/null || [ ! ${#fon} -eq 8 ] || [[ ! ${fon:0:1} == "9" ]]
then
echo "Invalid Phone Number"
elif ! grep -q $fon records
then
echo "Phone number not found"
else
grep $fon records
fi
while true
do
read -p "Confirm deletion: (y)es or (n)o: " answer
case $answer in
[Yy] ) grep -v "$answer" records >tempfile && rm records && mv tempfile records
echo "Record deleted."; break;;
[Nn] ) break;;
[Qq] ) exit 0;;
* ) echo "Please press y or n or q for exit.";;
esac
done
;;
Q)
break
;;
*)
echo Invalid selection
;;
esac
done