-   Programming (
-   -   BASH: Checking Users Input To File (

gangsta77 11-27-2011 07:52 PM

BASH: 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

Telengard 11-27-2011 08:27 PM


tmp$ cat carMakes.txt
tmp$ grep -w 'Honda'=$(cat carMakes.txt)
grep: Ford: No such file or directory
grep: Mercury: No such file or directory
grep: Nissan: No such file or directory
grep: BMW: No such file or directory

I see a number of problems here, not the least of which are that grep and test need to burn in a fire.
  • I think the syntax of your grep is not correct. Someone please correct me if I'm wrong about that.
  • Oh while I'm at it, this is an egregious UUOC. The final argument to grep should be a list of files to be searched with your regular expression. Those files don't exist.
  • In your test command you are testing for string equality. Don't you want to test for numerical equality (ie with -eq instead of =)?

grail 11-27-2011 11:58 PM

Well you will need to do more to provide an error message, but maybe this will get you started:


until grep -q $car1 car_file
    read -p "Please enter a car model: " car1

echo "$car1 is in the file"

All times are GMT -5. The time now is 03:17 PM.