[ale] Silly bash question

Ed Cashin ecashin at noserose.net
Thu Nov 12 15:51:18 EST 2009


On Thu, Nov 12, 2009 at 2:54 PM, Mills John M-NPHW64
<Jmills at motorola.com> wrote:
> Stephen -
>
> Thanks for closing this open item. It always takes me a while to sort out
> 'awk'!

You could avoid potential false positives by using awk even more.  The
pid you're looking for could appear in the line embedded in another pid
or even in the name or arguments of the command, but awk can operate
on the pid field itself without any potential for confusion.

  for pid in `jobs -p`; do
             `ps aux | awk -vp=$pid '$2==p{print $11}'`
             echo "${JOBNAME}: $pid";
  done

That also avoids false negatives that legitimately happen to have "grep"
in the ps line.  The "-v" option to awk allows convenient variable passing
that can simplify quoting issues.

I read something recently about how it is not necessary to put double
quotes around backticks.  I forgot where, although I rely on that.  Given
that fact, you can just do,

  for pid in `jobs -p`; do
             echo `ps aux | awk -vp=$pid '$2==p{print $11}'`: "$pid"
  done

-- 
  Ed Cashin <ecashin at noserose.net>
  http://noserose.net/e/
  http://www.coraid.com/


More information about the Ale mailing list