views:

30

answers:

3

I've just come off the Winows wagon, and the gnome-terminal and bash are looking great, but I don't quite know how to get it to do what I want, (I suspect it is possible).
Can std output (eg. from sed) be made to run as a command?
(ie. interpret and run the output as part of the script's logic.)
I am polling a process to output its status at timed intervals, and I would like to do it as a one liner.

# dd is already running in another terminal. Its PID is 1234
pgrep -l '^dd$' | sed -n 's/^\([0-9]\+.*\)/sudo watch -n 30 kill -USR1 \1/p'  
# this outputs: sudo watch 30 kill -USR1 1234  

Is there some short-cut terminal/bash magic to run sudo watch 30 kill -USR1 1234? Thanks.

+1  A: 

You should be able to wrap it in $():

$(pgrep ... | sed ...)

But why not do:

while :; do sleep 30; clear; kill -USR1 $(pgrep '^dd$'); done
Dennis Williamson
Cool! I thought it had to be easy.... $(echo echo fred) :)
fred.bear
Why not "while"? ... because when looking for a way to show the progress of `dd` that was what the snippet showed, and I wasn't game to venture too far off my know track (not with dd!)... but I think I'd be wise to take heed of a StackAthlon Grand Master's suggestion... :) I'll give "While" a whirl...
fred.bear
+1  A: 

Wrap it inside $(). $() assigns standard output like an environment variable. On a line by itself, this runs the printed command.

$(pgrep -l '^dd$' | sed -n 's/^\([0-9]\+.*\)/sudo watch -n 30 kill -USR1 \1/p')
Robert Wohlfarth
+1  A: 

Just pipe the commands to sh:

pgrep -l '^dd$' | sed -n 's/^\([0-9]\+.*\)/sudo watch -n 30 kill -USR1 \1/p' | sh
Bart Sas
Thanks... I didn't realize that you could pipe to sh... That's neat. I'll use $() this time, but its good to know about piping to sh, and it will likely come in handy sometime..
fred.bear