-   Linux - Software (
-   -   Shell Script: Find "Word" Run "Command" (

granatica 06-11-2004 06:14 PM

Shell Script: Find "Word" Run "Command"

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.



Tinkster 06-11-2004 06:27 PM


tail -n 100 /var/log/somelog|grep someword
if [ $? == "0" ]; then

granatica 06-11-2004 06:42 PM

It Worked!
Thanks a ton Tinkster!!!

Tinkster 06-11-2004 06:45 PM

welcome :}

nbhalala 09-11-2006 09:20 AM

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.

gloriant 07-25-2007 07:42 AM

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




# 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;
#real action
  tail -f ${if}|\
  awk -W interactive "/${wd}/{print \"${action}\"; fflush();}"|\
#end of real action

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.

"date +'%H:%M:%S';"\
"echo discovered pattern ${wd} in ${if};"\
"echo That discovery was on the \" FNR \"th line read;"\
"sleep 1; ";

All times are GMT -5. The time now is 03:06 PM.