LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-11-2004, 06:14 PM   #1
granatica
LQ Newbie
 
Registered: May 2003
Posts: 16

Rep: Reputation: 0
Shell Script: Find "Word" Run "Command"


Hello,

I am trying to write a simple script but I am very new to shell scripting. Can someone help? Here's what I need:

tail "somelog"
if word "someword" is found run "somecommand"

Seems simple enough to me. So far i have:

tail -n 100 /var/log/somelog|grep someword

Now I want to read the output of that command and if "someword" is found in "somelog" then run "somecommand", otherwise quit.

Thanks,

Jason
 
Old 06-11-2004, 06:27 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Code:
!#/bin/bash
tail -n 100 /var/log/somelog|grep someword
if [ $? == "0" ]; then
  somecommand
fi
 
Old 06-11-2004, 06:42 PM   #3
granatica
LQ Newbie
 
Registered: May 2003
Posts: 16

Original Poster
Rep: Reputation: 0
It Worked!

Thanks a ton Tinkster!!!
 
Old 06-11-2004, 06:45 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
welcome :}
 
Old 09-11-2006, 09:20 AM   #5
nbhalala
LQ Newbie
 
Registered: May 2004
Posts: 10

Rep: Reputation: 0
what to do If I'm using tail -f instead of tail -n.
My script (which is gaing tail) should not get stop. Is should continueously keep searching on /var/log/messages as it's getting updated frequently.

thanks in advance.
Naresh
 
Old 07-25-2007, 07:42 AM   #6
gloriant
Member
 
Registered: Sep 2004
Distribution: Debian{Woody,Sarge,Etch}, UbuntuLTS6.06, SuSE{6.2,8.0}
Posts: 42

Rep: Reputation: 16
maybe a bit late, but you could try a combination of awk to produce the instructions to execute, and pipe them into bash (to execute them).

Code:
#!/bin/bash

if=/somepath/somelog;
wd=someword;

# instruction to execute each time someword is found in somelog
action="echo 'pattern ${wd} found in ${if}';";

# change occasional '/' in wd into \/ this change is needed to allow
# usage of var wd between / and / in pattern for awk.
wd=$(echo "$wd"|sed -e "s#/#\\/#g;";);

if [ ! -f ${if} ]; then
  echo "file ${if} to monitor, not found." >&2;
else
#real action
  tail -f ${if}|\
  awk -W interactive "/${wd}/{print \"${action}\"; fflush();}"|\
  bash
#end of real action
fi
The real action part of the code functions like this:
1. tail
tail -f ${if} will read (in follow-mode) file ${if}. Output of this (so every line) is passed on (as input) to [i]awk.

2. awk
reads (lines) from stdin and applies instructions to them. The indication /pattern/{instructions}; will only execute instruction if /pattern/ matches something on the input-line. This removed the need for a grep-call. Now *if* the pattern matches, the instructions are executed. The {print \"${action}\"; fflush();} instruction will, everytime a line holding the pattern is received, print the string defined in action (earlier in the script), and fflush() will flush the output-buffer, causing no buffering (should be done by the -W interactive option, but this option apparently is not supported by gawk (in cygwin).
Now this awk will output the instructions when one wants them to be executed (when a pattern is discovered in the file).

3. bash
Will read from it's input (that's provided by the awk) and consider these lines to be instructions, so execute them.

fidgeting a bit with quotes and so, one could for instance in the script set action to more complex stuff (also read man awk to look further into the possibilities of awk.
Code:
action=\
"date +'%H:%M:%S';"\
"echo discovered pattern ${wd} in ${if};"\
"echo That discovery was on the \" FNR \"th line read;"\
"sleep 1; ";

Last edited by gloriant; 07-25-2007 at 08:27 AM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
Not able to Run/find "configure" command aaryan72 Linux - Newbie 4 09-10-2005 11:16 AM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM
Help,I can't find my USB mouse when I run "kudzu" command in RH9. aeolus Red Hat 0 03-27-2004 09:14 PM
1. shell script "find and replace" on text 2. java GUI application randomx Programming 4 03-05-2004 01:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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