matching user input to a text file loop?

       
until [ $cars1 -eq cars_names.txt ]
do
read -p "Invalid cars. Try againa" cars1
done

Ok i have the above code, im getting users input and if it doesnt match in the file the user has to try again untill its correct

But when i run this it gives me an error saying

./Cars.bash: line 43: [: ford: integer expression expected

You are using a numeric comparison (-eq) for strings. Use string comparison:

until [ "$cars1" = cars_names.txt ]
do
  read -ep "Invalid cars. Try again: " cars1
done
1 Like

Assumption: Your trying to validate user input against a list of valid responses (in cars_names.txt).

read -p "Car: " cars1
until grep -wiq "$cars1" cars_names.txt && [ -n "$cars1" ]
do
   echo "Invalid car. Try again"
   read -p "Car: " cars1
done
1 Like