Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'A perl version' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A perl version
Authored by: adamjacobmuller on Jun 21, '03 07:07:56PM

any of the previous versions would match too much
if you looked for login it would match loginwindow
or theloginprocessname
any word with login in it
this is not a good idea
also this version is case-insensitive
<code>
#!/usr/bin/perl
$search=lc($ARGV[0]);
@procs = `ps -cxa`;
for $proc (@procs ) {
if( $proc =~ /\s+(\d+)\s+\S+\s+\S+\s+\S+\s+(\S+)/ ) {
$pid = $1;
$name = lc($2);
if( $name =~ /^$search$/ ) {
print "$pid ";
}
}
}

print "\n";
</code>

and this version is case-sensitive
<code>
#!/usr/bin/perl
$search=$ARGV[0];
@procs = `ps -cxa`;
for $proc (@procs ) {
if( $proc =~ /\s+(\d+)\s+\S+\s+\S+\s+\S+\s+(\S+)/ ) {
$pid = $1;
$name = $2;
if( $name =~ /^$search$/ ) {
print "$pid ";
}
}
}

print "\n";
</code>



[ Reply to This | # ]