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: rselph on Jun 21, '03 11:33:05AM
Adam's solution is more elegant, and more succinct than mine. But I'm more of a perl hacker, than a shell programmer. If you want a version that will match based on a regular expression, instead of an exact match, you can use this perl script:
#!/usr/bin/perl

@procs = `ps -x`;

for $proc (@procs ) {
    if( $proc =~ /\s+(\d+)\s+\S+\s+\S+\s+\S+\s+(\S+)/ ) {
	$pid = $1;
	$name = $2;
	if( $name =~ /$ARGV[0]/ ) {
	    print "$pid ";
	}
    }
}

print "\n";


[ Reply to This | # ]
A perl version
Authored by: adamjacobmuller on Jun 21, '03 06:32:15PM

ok, 45 secs between posts sucks...


the whole point of life is to have options, perhaps i want to integrate your solution into my new-fangled perl script in osx?

your idea is great too and by writing it your also making a valuable contribution. in open source every little piece comes together and makes this gigantic unstoppable whole.



[ Reply to This | # ]
A perl version
Authored by: adamjacobmuller on Jun 21, '03 06:59:38PM

hrmmm I think that osxhints strips \'s this is fixed...
I also changed
ps -x
to
ps -cxa


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

print "\\n";
</code>



[ Reply to This | # ]
A perl version
Authored by: adamjacobmuller on Jun 21, '03 07:01:13PM

the last one was wrong as well... i double quoted it.

this is RIGHT!!! I swear!!!!

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

print "\n";
</code>



[ Reply to This | # ]
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 | # ]
A perl version
Authored by: adamjacobmuller on Jun 21, '03 07:19:19PM

the perl version is much faster too!
-------

case-insensitive bash&awk&tr version

bash-2.05a# time pidof httpd
337 348 385

real 0m0.169s
user 0m0.020s
sys 0m0.090s
----------------------------
bash&awk&tr case sensitive
bash-2.05a# time pidof httpd
337 348 385

real 0m0.094s
user 0m0.020s
sys 0m0.040s
----------------------------
perl case insensitive

bash-2.05a# time ./perl-pidof httpd
337 348 385

real 0m0.033s
user 0m0.000s
sys 0m0.030s
----------------------------
perl case sensitive
bash-2.05a# time ./perl-pidof-cs httpd
337 348 385

real 0m0.033s
user 0m0.010s
sys 0m0.020s

the verdict? pidof is fastest with the perl version which doesnt care if you want case sensitive or case insensitive output, what's my /bin/pidof now?
rselph's perl pidof function, case sensitive.



[ Reply to This | # ]