Addr Book Code in SHELL SCRIPT
#!/bin/sh
op=0
while [ "$op" -lt 7 ]; do
echo "Enter the option"
echo "1 for create"
echo "2 for add"
echo "3 for display"
echo "4 for search"
echo "5 for delete"
echo "6 for modify"
echo "7 for exit"
echo "Enter your choice:"
read op
case "$op" in
"1")
echo "Enter the name of database"
read db
touch "$db"
;;
"2")
echo "Enter the name of database"
read db
echo "Enter the number of records"
read n
it=0
while [ "$it" -lt "$n" ]; do
echo "Enter Id"
read id1
echo "Enter your name"
read nm
echo "Enter phone no."
read ph
if ! echo "$ph" | grep -q "^[0-9]\{10\}$"; then
echo "Invalid phone number format. Please enter 10 digits."
else
echo "$id1 $nm $ph" >> "$db"
it=$((it + 1))
fi
done
;;
"3")
echo "Enter name of db that you want to display"
read db
cat "$db"
;;
"4")
echo "Enter name of db from where you want to search"
read db
echo "Enter phone no."
read ph
if grep -q "$ph" "$db"; then
echo "Record found"
else
echo "Record not found"
fi
;;
"5")
echo "Enter name of db from where you want to delete"
read db
if [ -f "$db" ]; then
echo "Enter phone no. of the record you want to delete"
read ph
grep -v "$ph" "$db" > [Link]
mv [Link] "$db"
echo "Record deleted successfully!"
else
echo "Database not found."
fi
;;
"6")
echo "Enter name of db from where you want to modify"
read db
if [ -f "$db" ]; then
echo "Enter phone no. of the record you want to modify"
read ph
echo "Enter new Id"
read id1
echo "Enter new name"
read nm
echo "Enter new phone no."
read ph1
sed -i "/$ph/d" "$db"
echo "$id1 $nm $ph1" >> "$db"
echo "Record modified successfully!"
else
echo "Database not found."
fi
;;
"7")
echo "Exiting..."
break
;;
*)
echo "Invalid option. Please enter a number between 1 and 7."
;;
esac
done