output of system() call

Discussion in 'Perl Misc' started by Madhu Ramachandran, Jan 4, 2006.

  1. question.

    i am perl newbie. i read that system() call can be used to invoke other
    program (shell scripts etc). The return code of the program is returned by
    system() call and i have to >> by 8 bits.

    is there any variable which will contain ouput printed by the program in
    system() call?

    eg:
    system("mytest.sh");
    if mytest.sh has echo "bla" in it. how can i capture the 'bla' value, using
    the system() call?

    currently iam doing this.
    system ("mytest.sh > /tmp/outfile");
    # I open this /tmp/outfile and use $line = <outFp>; and get the value.

    wondering if there is an easier way to get output.

    -Madhu
     
    Madhu Ramachandran, Jan 4, 2006
    #1
    1. Advertising

  2. Madhu Ramachandran

    Joe Smith Guest

    Madhu Ramachandran wrote:

    > i am perl newbie. i read that system() call can be used to invoke other
    > program (shell scripts etc)


    Where did you read about the system() call? If it was part of any
    sort of decent tutorial, it should have told you right there the answer
    to your question.

    http://perldoc.perl.org/perlfaq8.html
     
    Joe Smith, Jan 4, 2006
    #2
    1. Advertising

  3. Madhu Ramachandran

    Paul Lalli Guest

    Madhu Ramachandran wrote:
    > i am perl newbie. i read that system() call can be used to invoke other
    > program (shell scripts etc). The return code of the program is returned by
    > system() call and i have to >> by 8 bits.
    >
    > is there any variable which will contain ouput printed by the program in
    > system() call?


    Read the documentation for the function you're using. It contains the
    answer to your question.

    perldoc -f system
    (3rd paragraph)

    Paul Lalli
     
    Paul Lalli, Jan 4, 2006
    #3
  4. Madhu Ramachandran <> wrote:


    > question.



    You put that in there to distinguish from the many posts here
    that do not ask a question?


    > i am perl newbie.



    You really should see the Posting Guidelines you know.


    > i read



    Where did you read it?

    In the documentation for the software that you are using?


    > that system() call can be used to invoke other
    > program


    > is there any variable which will contain ouput printed by the program in
    > system() call?



    No, but there is a way to capture the output printed by the program
    into a variable using something other than the system() function.


    > how can i capture the 'bla' value, using
    > the system() call?



    You can't.

    You'll have to use something other than the system() function.


    > wondering if there is an easier way to get output.



    Yes there is, and it is included in the documentation for the
    system() function that you already know about.

    Why didn't you read the documentation for the function you are using?


    By now you have used up all of your coupons...

    So long!


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 5, 2006
    #4
  5. Madhu Ramachandran wrote:
    > i am perl newbie. i read that system() call can be used to invoke
    > other program (shell scripts etc). The return code of the program is
    > returned by system() call and i have to >> by 8 bits.
    >
    > is there any variable which will contain ouput printed by the program
    > in system() call?


    Yep. It works exactly as described in paragraph 3, sentence 4 of the
    documentation of the very function that you are using.

    jue
     
    Jürgen Exner, Jan 5, 2006
    #5
  6. Madhu Ramachandran

    zentara Guest

    On Wed, 4 Jan 2006 10:45:42 -0500, "Madhu Ramachandran"
    <> wrote:

    >question.
    >
    >i am perl newbie. i read that system() call can be used to invoke other
    >program (shell scripts etc). The return code of the program is returned by
    >system() call and i have to >> by 8 bits.
    >
    >is there any variable which will contain ouput printed by the program in
    >system() call?
    >
    >eg:
    >system("mytest.sh");
    >if mytest.sh has echo "bla" in it. how can i capture the 'bla' value, using
    >the system() call?
    >
    >currently iam doing this.
    >system ("mytest.sh > /tmp/outfile");
    ># I open this /tmp/outfile and use $line = <outFp>; and get the value.
    >
    >wondering if there is an easier way to get output.


    Here is a super simple example using IPC::Open3. Of course,
    you don't need the bash, you can run mytest.sh directly.

    There are many more techniques around to enhance your use
    of IPC::Open3, google for them.
    #!/usr/bin/perl
    use warnings;
    use strict;
    use IPC::Open3;
    $|=1;

    #my $pid=open3(\*IN,\*OUT,\*ERR,'/bin/bash');
    my $pid=open3(\*IN,\*OUT,0,'/bin/bash');
    # set \*ERR to 0 to send STDERR to STDOUT

    my $cmd = 'date';
    #send cmd to bash
    print IN "$cmd\n"; #need the \n with bash

    #getresult
    my $result = <OUT>;
    print $result;

    __END__






    --
    I'm not really a human, but I play one on earth.
    http://zentara.net/japh.html
     
    zentara, Jan 5, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. 131.96.106.128 [johnny]

    grab system() call output

    131.96.106.128 [johnny], Jul 24, 2003, in forum: C++
    Replies:
    1
    Views:
    460
    Victor Bazarov
    Jul 24, 2003
  2. system call and library call

    , Aug 22, 2005, in forum: C Programming
    Replies:
    7
    Views:
    3,056
    Jonathan Bartlett
    Aug 23, 2005
  3. v4vijayakumar

    Extracting output from system(3) call

    v4vijayakumar, May 13, 2006, in forum: C Programming
    Replies:
    18
    Views:
    444
    Kevin Bagust
    May 15, 2006
  4. Wes Gamble
    Replies:
    2
    Views:
    123
    Robert Klemme
    Jun 19, 2009
  5. Jerry Mr
    Replies:
    10
    Views:
    221
    Brian Candler
    Oct 8, 2009
Loading...

Share This Page