prompt to delete each record when pattern is found

Hello!. I am working on a very simple program and I have been trying different things. This is so far what I have done and there is one small detail that still does not work. It finds all the records in a phonebook per say:

./rem Susan
More than one match; Please select the one to remove: 
Susan1 banana
Susan2 apple   remove (y/n)?
n

The problem is that it only prompts to remove the last record and skips the first record. BUT if I type "y" then it will remove both records from the phonebook file, if I type "n" then it removes none. Here is my program so far.

if [ "$#" -ne 1 ]
then
    echo "Incorrect number of arguments."
    echo "Usage: rem name"
    exit 1
fi

name=$1

#
# Find number of matching entries
#

matches=$(grep "$name" phonebook | wc -l)
names=$(grep "$name" phonebook | awk '{print $1" " $2}')
echo "$names" 
#p="$1"

#
# If more than one match, issue message, else remove it
#

if [ "$matches" -gt 1 ]
then
    
    echo "More than one match; Please select the one to remove: "
    for i in "$names"
     do
        echo "$i   remove (y/n)?"
        read ans
        if [ $ans != n ]
            then
            grep -v "$name" phonebook > /tmp/phonebook
            mv /tmp/phonebook phonebook
        fi

    done
    
elif [ "$matches" -eq 1 ]
then
    grep -v "$name" phonebook > /tmp/phonebook
    mv /tmp/phonebook phonebook
else
    echo "I couldn't find $name in the phone book"
fi

Does anybody would be so kind to tell me what is wrong with my program? I appreciate any help. Thank you, thank you.

I would have a look here first:

if [ $ans != n ]

n is unkonwn and it is not a variable
$ans is not being used later on

--
On a different note, I would change this:

grep -v "$name" phonebook > /tmp/phonebook
mv /tmp/phonebook phonebook

to this:

grep -v "$name" phonebook > /tmp/phonebook && mv /tmp/phonebook phonebook

In other words, only overwrite the original file if the operation was successful.

--
also, in future please use the code button and use proper indenting so your code becomes easier to read.

Thank you very much for your help Mr. Scrutinizer. Your second comment was very helpful, I made the change and that works perfect. I would like to explain my idea behind the line that you mentioned.
if [ $ans != n ] the variable $ans is the answer from the user (y/n) it suppose to enter the loop and prompt for every pattern found. Which means that IF the user types 'y' THEN it will proceed to remove the record #1, BUT, IF $ans = n THEN it will go to the line prinf "Record will not be removed" BUT still the program will go to the second pattern found IF any, and prompt me to remove the record going through the same process. I still cannot make it do that. Please that is the only detail. Thanks.

---------- Post updated at 05:59 PM ---------- Previous update was at 04:59 PM ----------

Here I wanted to put it this way for simplicity.

rnames=$(grep Susan phonebook | awk '{print $1" " $2}')

for i in "$rnames"; do echo "$i remove? (y/n)"; done
Susan1 banana
Susan2 apple remove? (y/n)

Stills puts the remove? question only at the end. What do i need to do to prompt for the first record?

The variable i is being specified as the variable in the for loop, but what is used inside the loop in the grep -v statement the variable name .

Further, you need to use for i in $names without the double quotes (otherwise it will loop only once for the whole string). But you would need to change the IFS variable in this case, since you are using multiple words for matching and you want to only use the newline character as the field separator.

Another option would be to use:

echo "$names" | 
while read i
do
  ....
done

I would use:

if [ "$ans" = "y" ]; then

so that only the letter y will lead to the deletion and not any character but n.

To allow case independence and multiple possible answers you could use this:

case $ans in
  y|Y|yes|YES) grep -v "$i" phonebook > /tmp/phonebook && mv /tmp/phonebook phonebook
esac

One more difficulty is that you are using:
names=$(grep Susan phonebook | awk '{print $1" " $2}')
So that there is only a single space between $1 and $2, even if there were for instance two spaces in the input file. If you then later would like to delete the line based on these two variables, grep will no longer find them

For example:

$ cat phonebook
Susan1 banana
Susan2    apple
$ grep "$name" phonebook | awk '{print $1" " $2}'
Susan1 banana
Susan2 apple
$ names=$(grep "$name" phonebook | awk '{print $1" " $2}')
$ echo "$names" | while read i; do echo "$i"; done
Susan1 banana
Susan2 apple
$ grep -v "Susan2 apple" phonebook
Susan1 banana
Susan2    apple