checking users input to file

ok im sorta stuck on this,

The user enters a car model and it has to check it in a text file in the current directory. If the car model is not in the file, user has to enter another model

This is what i have so far

read -p "Enter a car model: " car1
grep -w $car1=$(cat carMakes.txt)
test $? = 0 && echo "Car not found, try again please"

it executes but i get a long list saying, the following

grep: Honda No such file or directory
read -p "Enter a car model: " car1
grep -w $car1 carMakes.txt
test $? -ne 0 && echo "Car not found, try again please"
1 Like

ok thanks that works but say the first time i enter something wrong then i enter it wrong again, how do i keep making it loop until something correct is entered?

I changed the echo in the third line to read -p and added the variable at the end

read -p "Enter a car model: " car1
grep -w $car1 carMakes.txt
test $? -ne 0 && read -p "Car not found, try again please" car1

also when i enter the model name and its correct it repeats it on the next line

Use grep option -q to suppress output.

#! /bin/bash

while [ 1 ]
do
    read -p "Enter car model: " car1
    grep -wq $car1 carMakes.txt
    flag=$?
    if [ $flag -eq 0 ]
    then
        break
    else
        echo "Car name not found in file. Enter again."
    fi
done