backticks returning unexpected result

Discussion in 'Perl Misc' started by dn_perl, Jan 15, 2004.

  1. dn_perl

    dn_perl Guest

    I am using backticks in two places in a perl script. One instance
    is returning the expected result. The other one has me confused.

    1) my @list_of_ipcs = `ipcs -q -p | grep "0x" ` ;
    This command works the way I would expect it to.

    2) my @related_list = `ps -ef | grep $id_parent_proc | grep 16 ` ;
    Let us say, $id_parent_proc = "44556"
    The above statement creates @related_list as an array with two rows.
    If I run the command from the shell : ps -ef | grep 44556 | grep 16 ,
    it retruns only one line. I thought backticks would also create
    an array with just one column.

    But here is what I get :
    $related_list[0] ===> " userA 6161 6157 0 17:01:28 pts/1
    0:00 sh -c ps -ef | grep 44556 | grep 16"

    $related_list[1] ===> " userA 44556 1 0 Dec 22 ?
    0:01 daemon_proc 16 /vol1/apps/mydir................. "

    How can I eliminate that 0-th entry in the @related_list array ?

    One more question while I am it.



    When I run : ps -ef | grep 5000
    I almsot always get at least one row in return, the row which says :
    userA 6598 5464 0 17:07:36 pts/1 0:00 grep 5000
    But sometimes I don't find any row returned as response.
    What could be the reason?

    TIA.
     
    dn_perl, Jan 15, 2004
    #1
    1. Advertisements

  2. dn_perl

    Ben Morrow Guest

    Don't do that. Use Proc::processTable from CPAN, or process the output
    from ps properly, or, at least, use

    my @related_list = grep { /$id_parent_proc/ and /16/ } `ps -ef`;

    .. What's the 'grep 16' there for anyway? Except for false positives
    like the first line, only one process can possible match; pids being
    unique 'n all.
    This Is Not A Perl Question. What is happening is that sometimes the
    ps process finishes building its list of processes before the grep
    process is even started, so it doesn't appear in the list. If it isn't
    there to start with, grep can't pick it out.

    Ben
     
    Ben Morrow, Jan 15, 2004
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.