LinuxQuestions.org
Help answer threads with 0 replies.
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 10-25-2011, 01:12 PM   #1
huntaz556
LQ Newbie
 
Registered: Oct 2011
Posts: 22

Rep: Reputation: Disabled
Read a text file with bash


Im making a bash script and i need to check a text file to see if what someone entered is already there , if it is already there then i want the script to say you have already entered this are you sure? but im not sure how to do that can you help me please ,

thank you!
 
Old 10-25-2011, 01:22 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
You can use the grep command for that. For example
Code:
grep -q $USERINPUT textfile
This will search for the text in the variable $USERINPUT in the file textfile without printing the found matches (if any, option -q). grep will return an exit code of 0 if the text is found, otherwise it will return exit code 1.

For more info on grep use
Code:
man grep

Last edited by TobiSGD; 10-25-2011 at 01:23 PM.
 
Old 10-25-2011, 01:36 PM   #3
huntaz556
LQ Newbie
 
Registered: Oct 2011
Posts: 22

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TobiSGD View Post
You can use the grep command for that. For example
Code:
grep -q $USERINPUT textfile
This will search for the text in the variable $USERINPUT in the file textfile without printing the found matches (if any, option -q). grep will return an exit code of 0 if the text is found, otherwise it will return exit code 1.

For more info on grep use
Code:
man grep
thank you that works for searching the file but how do i implement this into my code and it will be between an if and elif statement thanks !
 
Old 10-26-2011, 03:49 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
Try putting it in your if statement ... you will be pleasantly surprised
 
Old 10-27-2011, 06:02 AM   #5
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Further note that in your future bash script, $? will return contain the return value of the last executed command.

OK
 
Old 10-27-2011, 07:02 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
Quote:
Further note that in your future bash script, $? will return contain the return value of the last executed command.
But not required if you follow my advice
 
  


Reply

Tags
bash, file, text


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
[BASH] How to read multiple lines from a text file? gtirsmiley Programming 13 03-11-2011 10:18 AM
How to read a text file using a bash script Jeroen1000 Programming 8 09-30-2009 06:53 AM
how to read text file using bash script kkpal Linux - Newbie 4 03-12-2008 01:57 AM
how to read text file using bash script kkpal Linux - Newbie 2 03-03-2008 11:40 AM
Read variables from a text file in Bash jakev383 Linux - General 5 12-20-2006 07:29 AM

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

All times are GMT -5. The time now is 03:48 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