Unix Shell - Execute a command based on output of another command

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2006 > Execute a command based on output of another command





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Execute a command based on output of another command
Sashi

2006-01-18, 5:55 pm

Hi, I'm running a listener on Tibco RV. Every time I capture a message,
I need to run a shell script.
Here's the command I use:
$ tibrvlisten [parameters] MY_TEST_SUBJECT
(If you're not familiar with RV, no big deal. Suffice it to say that
whenever the above command is run, it'll wait for incoming messages,
capture messages when they arrive and print them.)
Can anyone give me an idea of how to capture the output from another
shell script and run my script when this listen command spews some
output?
Put another way, I need to monitor this process (running in the
background) and run my script whenever this process spews its output.
TIA,
Sashi

Chris F.A. Johnson

2006-01-18, 5:55 pm

On 2006-01-18, Sashi wrote:
> Hi, I'm running a listener on Tibco RV. Every time I capture a message,
> I need to run a shell script.
> Here's the command I use:
> $ tibrvlisten [parameters] MY_TEST_SUBJECT
> (If you're not familiar with RV, no big deal. Suffice it to say that
> whenever the above command is run, it'll wait for incoming messages,
> capture messages when they arrive and print them.)
> Can anyone give me an idea of how to capture the output from another
> shell script and run my script when this listen command spews some
> output?
> Put another way, I need to monitor this process (running in the
> background) and run my script whenever this process spews its output.


tibrvlisten [parameters] MY_TEST_SUBJECT | my_script


--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Sashi

2006-01-18, 5:55 pm


> tibrvlisten [parameters] MY_TEST_SUBJECT | my_script
>
>
> --
> Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
> Shell Scripting Recipes: | My code in this post, if any,
> A Problem-Solution Approach | is released under the
> 2005, Apress | GNU General Public Licence


Chris, that doesn't work. my_script ends up running even without the
tibrvlisten command capturing any messages.
Sashi

Chris F.A. Johnson

2006-01-18, 5:55 pm

On 2006-01-18, Sashi wrote:
>
>
> Chris, that doesn't work. my_script ends up running even without the
> tibrvlisten command capturing any messages.


This will call your script only when a line is received from
tibrvlisten:

tibrvlisten [parameters] MY_TEST_SUBJECT |
while IFS= read -r line
do
printf "%s\n" "$line" | my_script
done

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Sashi

2006-01-18, 5:55 pm


Chris F.A. Johnson wrote:
> On 2006-01-18, Sashi wrote:
>
> This will call your script only when a line is received from
> tibrvlisten:
>
> tibrvlisten [parameters] MY_TEST_SUBJECT |
> while IFS= read -r line
> do
> printf "%s\n" "$line" | my_script
> done
>
> --
> Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
> Shell Scripting Recipes: | My code in this post, if any,
> A Problem-Solution Approach | is released under the
> 2005, Apress | GNU General Public Licence


Thanks, that worked.
Sashi

Sashi

2006-01-23, 6:13 pm


Chris F.A. Johnson wrote:
> On 2006-01-18, Sashi wrote:
>
> This will call your script only when a line is received from
> tibrvlisten:
>
> tibrvlisten [parameters] MY_TEST_SUBJECT |
> while IFS= read -r line
> do
> printf "%s\n" "$line" | my_script
> done
>
> --
> Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
> Shell Scripting Recipes: | My code in this post, if any,
> A Problem-Solution Approach | is released under the
> 2005, Apress | GNU General Public Licence


Revisiting this post, this script runs "my_script" script twice. Any
ideas why?
Thanks,
Sashi

Chris F.A. Johnson

2006-01-23, 6:13 pm

On 2006-01-23, Sashi wrote:
>
> Chris F.A. Johnson wrote:
>
> Revisiting this post, this script runs "my_script" script twice. Any
> ideas why?


Presumably, because tibrvlisten prints two lines. If you don't want
that to happen, don't use a while loop:

tibrvlisten [parameters] MY_TEST_SUBJECT | {
IFS= read -r line
printf "%s\n" "$line" | my_script
}

Or test the contents of line before calling the script.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com