Go Job Hunting at the LQ Job Marketplace
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-27-2011, 06:52 PM   #1
LQ Newbie
Registered: Feb 2011
Posts: 10

Rep: Reputation: 0
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
Old 11-27-2011, 07:27 PM   #2
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
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 =)?

Last edited by Telengard; 11-27-2011 at 07:37 PM.
Old 11-27-2011, 10:58 PM   #3
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 8,111

Rep: Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185Reputation: 2185
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"


bash, grep, shell script, test

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash checking for file write completion before acting? gary_in_springhill Programming 10 01-22-2011 06:43 AM
Bash: Handling input from file and keyboard moo-cow Programming 4 01-24-2009 08:21 AM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
bash - Checking for file existance? rignes Programming 5 02-18-2006 03:02 PM
Can't add new users from an input file hindll01 Programming 4 09-07-2004 07:32 PM

All times are GMT -5. The time now is 01:57 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration