Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perlmonks,

I need to call a shell script in Perl like `xyz abcd`.
xyz - shell script
abcd - parameter (program name)

"xyz" script is to determine if any program name and parameter,if any, passed in is actually a running process and if so, returns count of running instances.

When I call $count=`xyz abcd` in a perl program, it should return "1". But it returns "2".

Below are the running instances,
sh /test/xyz abcd
/usr/bin/perl ./abcd.pl

Could you please guide me to suppress "sh /test/xyz abcd"?

Thnaks for your help.

Janitored by holli - removed pre tags

Replies are listed 'Best First'.
Re: Call shell script in Perl
by polettix (Vicar) on May 26, 2005 at 09:23 UTC
    One trick that you can use is to wrap one character inside square brackets:
    $count=`xyz [a]bcd`
    The shell will interpret "[a]" as "a", but in the process list it shows up as "[a]bcd" and will not be matched.

    Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

    Don't fool yourself.

      The "Most Ingenious Answer of the Day Award" goes to frodo72. Your prize is a highly coveted upvote from me.

          -Bryan

        Now that you gave the prize to me, I can confess that it's not my invention - I saw it in some grep-related documentation a lot of time ago. Maybe you can mentally turn it into a "Memory Award"!

        Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

        Don't fool yourself.
Re: Call shell script in Perl
by Random_Walk (Prior) on May 26, 2005 at 08:04 UTC

    I guess you are doing something like ps -ef | grep -c progname which will always find its own instance. You can either alter your script so it does a something like | grep -v grep before it counts or as it will find itself with 100% reliabilty you can just --$count to get the correct value.

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: Call shell script in Perl
by ivancho (Hermit) on May 26, 2005 at 08:13 UTC
    you may want to look up Proc::ProcessTable and skip the shell stuff entirely.. you could probably count processes by fname..

    Update: Bah, so late...

Re: Call shell script in Perl
by blazar (Canon) on May 26, 2005 at 08:06 UTC
Re: Call shell script in Perl
by Roger (Parson) on May 26, 2005 at 08:56 UTC
    I guess what your shell script does is probably along the lines of ps -ef | grep abcd? You could do this in Perl entirely, without calling external scripts. You can either call ps -ef, or you could use one of the CPAN modules, such as Proc::ProcessTable or Unix::Process, and do the filtering/counting of program instances yourself. This will give you precise control over what to and what not to select from the process table.