LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 12:05 AM   #1
plisken
Member
 
Registered: Dec 2001
Location: Scotland
Distribution: Slackware 9.1-15 RH 6.2/7, RHEL 6.5 SuSE 8.2/11.1, Debian 10.5
Posts: 516

Rep: Reputation: 32
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, 12:20 AM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Try:
Code:
[ "`grep -c STRING FILE`" -gt "0" ] && command to execute
 
Old 07-27-2005, 05:58 AM   #3
APB_4
Member
 
Registered: Nov 2004
Distribution: KDE Neon User edition; Manjaro; OpenSUSE Leap
Posts: 298

Rep: Reputation: 31
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, 08:35 AM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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, 01:36 PM   #5
plisken
Member
 
Registered: Dec 2001
Location: Scotland
Distribution: Slackware 9.1-15 RH 6.2/7, RHEL 6.5 SuSE 8.2/11.1, Debian 10.5
Posts: 516

Original Poster
Rep: Reputation: 32
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, 01:51 PM   #6
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
&& 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 01:54 PM.
 
Old 07-28-2005, 11:06 AM   #7
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

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


Reply



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 08:47 PM
remote command execute telnet with 1 command MPowers Linux - Networking 2 06-30-2005 06:31 AM
how to grep only one string pr match gummimann Linux - General 3 11-06-2003 09:40 AM
grep - finding string and replacing with new ckibler Linux - Newbie 6 08-01-2003 06:25 PM
grep command cuss Linux - General 7 02-14-2003 09:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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