I am new to Bash and was wondering if someone could give me some insight on how I can make this program work more accurately.
Goal: To write a bash shell script that presents a menu to the user with the options of add a contact, remove a contact, find a contact, list all contacts, and exit the program.
This is my code so far:
#!/bin/bash
touch contacts.dat
echo "Select one of the following options:"
echo "-------------------------------------"
echo "1. Add a contact"
echo "2. Remove a contact"
echo "3. Find a contact"
echo "4. List all contacts"
echo "5. Exit the program"
read -p "Enter a choice (1-5): " choice
echo
#
case "$choice" in
1)
read -p "Enter a name: " name
read -p "Enter an email address: " email
echo "$name , $email" >> contacts.dat
;;
2)
read -p "Enter a name: " name
if (grep -q name contacts.dat) then
grep -v name contacts.dat > deletedNames.dat
echo "$name was removed from the file."
else
echo "$name does not exist in the file."
fi
;;
3)
read -p "Enter a name: " name
if (grep -q name contacts.dat) then
echo "$name , $email"
else
echo "The $name was not found."
fi
;;
4)
sort -k 1 contacts.dat
;;
5)
echo "Thank you for using this program!"
# break?
exit 1
;;
*)
echo "Please enter a valid choice (1-5)."
;;
esac
The program seems to work with options 1, 4, and 5. However, not with 2 and 3.
How can I get 2 and 3 to remove the contact and find the contact (respectfully)? Thank you in advance for any help you may be able to offer.