LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 07-27-2005, 01:05 AM   #1
plisken
Member
 
Registered: Dec 2001
Location: Scotland
Distribution: Slackware 9.1/13.37/14 RedHat 6.2/7/EL6.5 SuSE 8.2/11.1
Posts: 459

Rep: Reputation: 30
Question use grep and execute a command if string is found


I'm looking to search a file for a string and if that string is found at least once in the file then I'm looking to perform an action like so:

grep -q "some_string" my_file [if TRUE perform Command]
ie. if my_file contains some_string then perform the command and if not, do nothing.

I know you can do this sort of thing with find and -exec but hoq so with grep?

I'm sure there will be a VERY simply explination, so please go for it.

Thanks in advance...
 
Old 07-27-2005, 01:20 AM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Try:
Code:
[ "`grep -c STRING FILE`" -gt "0" ] && command to execute
 
Old 07-27-2005, 06:58 AM   #3
APB_4
Member
 
Registered: Nov 2004
Location: England
Distribution: Ubuntu 9.04; Crunchee; EEE Xandros
Posts: 289

Rep: Reputation: 30
Hey Matir won't that execute the command whatever? How about something like this:

Code:
if grep -q "some_string" my_file; then perform COMMAND; fi
 
Old 07-27-2005, 09:35 AM   #4
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
It will only perform the command if grep found one or more matches. Alternatively (deriving from yours) you could do:
Code:
grep -q string file && command
 
Old 07-27-2005, 02:36 PM   #5
plisken
Member
 
Registered: Dec 2001
Location: Scotland
Distribution: Slackware 9.1/13.37/14 RedHat 6.2/7/EL6.5 SuSE 8.2/11.1
Posts: 459

Original Poster
Rep: Reputation: 30
grep -q string file && command

Excellent, this is EXACTLY what I am looking for, knew it would be simple...

I also assume ir I run:

grep -q string file ; command

It will execute the command regardless of a TRUE condition.

I know & allows something to run in the backgroiund, but what exactly does the && mean?

Right now, thanks though...
 
Old 07-27-2005, 02:51 PM   #6
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
&& means AND.
|| means OR.

How it works is this:
1) Bash sees the && and realizes "ah, I'm evaluating a conditional expression". It doesn't matter that we'll never do anything with the combined condition.
2) Bash executes the first part (before the &&) and makes a decision: if the first part returns non-zero (an error) it realizes "well, false and any value return false, so let's skip executing the second part". However, if it returns 'true' (0), bash knows it has to evaluate the output of the second part to determine the final condition.

In other words, suppose we have:
command1 && command2

Bash would see it like this:
Code:
if RESULT_OF_COMMAND1 == FALSE
    return FALSE
else
    if RESULT_OF_COMMAND2 == FALSE
        return FALSE
    else
        return TRUE
    endif
endif
(That's just pseudocode, btw, don't try to execute it) If COMMAND1 gives false, then it never tries to get a "result of command 2".

I hope that's cleared things up a bit.

Last edited by Matir; 07-27-2005 at 02:54 PM.
 
Old 07-28-2005, 12:06 PM   #7
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
Thanks for the reminder of grep's '-q' option -- I was about to put foot in mouth by suggesting you add >/dev/null.
 
  


Reply


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script: grep using string with specail charactors? Chowroc Programming 6 11-14-2005 09:47 PM
remote command execute telnet with 1 command MPowers Linux - Networking 2 06-30-2005 07:31 AM
how to grep only one string pr match gummimann Linux - General 3 11-06-2003 10:40 AM
grep - finding string and replacing with new ckibler Linux - Newbie 6 08-01-2003 07:25 PM
grep command cuss Linux - General 7 02-14-2003 10:23 AM


All times are GMT -5. The time now is 05:31 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration