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; ";