LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 11-27-2011, 06:52 PM   #1
gangsta77
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

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

Code:
grep: Honda No such file or directory
 
Old 11-27-2011, 07:27 PM   #2
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
Code:
tmp$ cat carMakes.txt
Honda
Ford
Mercury
Nissan
BMW
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
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Well you will need to do more to provide an error message, but maybe this will get you started:
Code:
car1='nocar'

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

echo "$car1 is in the file"
 
  


Reply

Tags
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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:24 AM.

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