in reply to spaces in grep command argument executed in backticks

The third field of ps -ef is the parent pid, did you mean that? Also, you can avoid the extra grep -v with a simple RE trick:
$module = "xyz ab[c]"; $pid = `ps -aef|grep \"$module\"|cut -d ' ' -f3`;
Note the [ ] around one of the characters.

Back to perl, here is a simple one-liner (OK, three lines) which only creates one child process:
my $module = 'xyz abc'; my @ppids = map {(split)[2]} grep /$module/,`ps -ef`; print "@ppids\n";
If you really wanted the pid rather than the ppid then change the [2] to [1].
Notice that I am using an array, since there might be more than one process which matches.

Replies are listed 'Best First'.
Re^2: spaces in grep command argument executed in backticks
by jwkrahn (Abbot) on Oct 23, 2009 at 21:01 UTC
    my @ppids = map {(split)[2]} grep /$module/,`ps -ef`;
    my @ppids = map /$module/?(split)[2]:(), `ps -ef`;