LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Read a text file with bash (http://www.linuxquestions.org/questions/programming-9/read-a-text-file-with-bash-910099/)

huntaz556 10-25-2011 02:12 PM

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!

TobiSGD 10-25-2011 02:22 PM

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

huntaz556 10-25-2011 02:36 PM

Quote:

Originally Posted by TobiSGD (Post 4508003)
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 !

grail 10-26-2011 04:49 AM

Try putting it in your if statement ... you will be pleasantly surprised :)

AnanthaP 10-27-2011 07:02 AM

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

OK

grail 10-27-2011 08:02 AM

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


All times are GMT -5. The time now is 02:07 AM.