我是Bash的新手,并且想知道是否有人可以让我了解如何让这个程序更准确地运作。
目标:编写一个bash shell脚本,向用户显示一个菜单,其中包含添加联系人,删除联系人,查找联系人,列出所有联系人以及退出程序的选项。
到目前为止,这是我的代码:
#!/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
该程序似乎适用于选项1,4和5.但是,不是2和3。
如何获得2和3删除联系人并找到联系人(尊敬地)?提前感谢您提供的任何帮助。