On Thu, Oct 30, 2003 at 12:26:30PM +1300, Volker Kuhlmann wrote:
> The ps method is a cludge at best, and a bad one at that because it
> also catches processes started by other shells under the same uid.

> > That'd be a simple
> > jobs -p %x

> Does the %NUMBER bit work the same in scripts?

Yes, if you enable job control (aka monitor mode).  Try 'set -m' in a
script.  See below, and try to ignore the abuse of ps(1) for now.  I'm
not sure it's a good idea to use job control in scripts, but it can be
done.

----------------------------------8<------------------------------------
#!/usr/bin/env bash

# enable job control
set -m

# cat a multiline file to a slow pipe reader
cat somefile | perl -ne 'print; sleep 1;' > /dev/null &

sleep 1
echo 'should see a cat and a perl process'
ps ax | egrep 'cat|perl'

sleep 1
echo 'kill the pipeline'
kill %1

sleep 1
echo 'cat and perl should be gone'
ps ax | egrep 'cat|perl'
----------------------------------8<------------------------------------

> How do I get the exit code of those processes?

In bash, use the PIPESTATUS array once the pipeline exits.

Cheers,
-mjg
-- 
Matthew Gregan                     |/
                                  /|                [EMAIL PROTECTED]

Reply via email to