At some point in this bla | foo | kitchen | sink -, ahem, -command i lost the ability to picture a possible outcome. Could you please explain what this supposed to achieve? Or let it run on the system it was designed for and show some sample outcome.
One point i immediately saw was that ps -ef | awk ... | sed ... must be nonsense because either use awk or use sed and furthermore probably both are superfluous because the output of ps cat be tailored using the -o option. See the man page for details.
ps -ef | awk -v gpid="$gpid" '
{ line[NR] = $0
pid[NR] = $2
ppid[NR] = $3
}
$2 == gpid || $3 == gpid {
PrintPid[$2]
PrintPid[$3]
}
END { for(i = 1; i <= NR; i++)
if(pid in PrintPid || ppid in PrintPid)
print line
}'
but, of course, ps -ef output may vary somewhat from operating system to operating system and you haven't told us what OS you're using. And, if you're using a Solaris/SunOS system, you'll need to change awk to /usr/xpg4/bin/awk or nawk .
post#4 is unprecise (like post#1), for example will find pid 22 when searching for 2.
While the overall sense is not clear for me, this is certainly *not* intended.
Fix:
No. It doesn't give the same result as the code in post #3. With slightly modified versions of these scripts: ./tester :
#!/bin/ksh
gpid=${1:-$$}
ps -ef | tee ps.out | awk -v gpid="$gpid" '
{ line[NR] = $0
pid[NR] = $2
ppid[NR] = $3
}
$2 == gpid || $3 == gpid {
PrintPid[$2]
PrintPid[$3]
}
END { for(i = 1; i <= NR; i++)
if(pid in PrintPid || ppid in PrintPid)
print line
}'
Note that this captures the output from ps in ps.out which will be used as input for the other two scripts instead of rerunning ps . The above script produces the output:
MadeInGermany's simpler code doesn't catch the parent, sibling, or grandchild processes of the process specified by $gpid . It only catches the process specified by $gpid and its children.
I do, however, agree that SkySmart's code works by accident when $gpid specifies a PID that isn't small enough to accidentally match several other unintended PIDs of unrelated running processes.