Get output from system()

Discussion in 'Perl Misc' started by Perl Learner, Jun 22, 2005.

  1. Perl Learner

    Perl Learner Guest

    hi there again

    in my program, i am trying to check if gnuplot is installed on the
    system

    for that, i have been thinking of doing

    system("which gnuplot")

    and, depending on the output i get from it, find out whether or not it
    is installed.

    in linux, if the software isn't installed, the $status variable is set
    to 1. but this isn't the same (atleast) on (these) Sun machines. so
    now i can not rely on the $status variable.

    so i want to take the output from the terminal and do some parsing.
    but how do i get that output (when i do system("which gnuplot")) into
    my perl program?
     
    Perl Learner, Jun 22, 2005
    #1
    1. Advertising

  2. "Perl Learner" <> wrote in
    news::

    > in my program, i am trying to check if gnuplot is installed on the
    > system
    >
    > for that, i have been thinking of doing
    >
    > system("which gnuplot")


    ....

    > so i want to take the output from the terminal and do some parsing.
    > but how do i get that output (when i do system("which gnuplot")) into
    > my perl program?


    It seems fairly obvious to me that if want to learn more about the
    system function, then you check the documentation for the system
    function:

    perldoc -f system

    You might want to consult the posting guidelines for this group to learn
    how you can help yourself, and help others help you.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jun 22, 2005
    #2
    1. Advertising

  3. Perl Learner <> wrote:


    > hi there again



    You need to learn to try searching the standard docs *before* asking
    hundreds of people around the world to help you with your problem.


    > how do i get that output (when i do system("which gnuplot")) into
    > my perl program?



    The way that the documentation for the function that you are
    using says to:

    perldoc -f system

    This is not what you want to use to capture the output
    from a command, for that you should use merely ...


    Using a function without reading its docs is the programming
    equivalent of signing a contract without reading it.

    You are asking for trouble if you continue to sign without reading
    the contract first.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 22, 2005
    #3
  4. Perl Learner wrote:
    [...]
    > system("which gnuplot")
    >

    [...]
    > but how do i get that output (when i do system("which gnuplot")) into
    > my perl program?


    Exactly the way it is described in the FAQ ('perldoc -q system', 'perldoc -q
    output'):
    "Why can't I get the output of a command with system()?"
    and exactly the way it has been explained in this NG over and over again
    and exactly as it is defined in the documentation of the very function you
    are using ('perldoc -f system'; paragraph 3, sentence 3).

    jue
     
    Jürgen Exner, Jun 23, 2005
    #4
  5. Perl Learner

    Perl Learner Guest

    i did look at

    perldoc -f system

    and i didn't understand what they meant by "backticks" (although, it
    now seems fairly obvious from the name itself. i didn't know what they
    meant by that when i first read it and i hence posted the question
    here)

    anyway, i am posting the solution here so someone would find it useful
    in the future


    $terminaloutput = `whoami`;
    print "you are $terminaloutput";
     
    Perl Learner, Jun 23, 2005
    #5
  6. Perl Learner

    Paul Lalli Guest

    Please quote an appropriate amount of context when replying. Not
    everyone reads messages in threaded format. Thank you.

    Perl Learner wrote:
    > i did look at
    >
    > perldoc -f system
    >
    > and i didn't understand what they meant by "backticks" (although, it
    > now seems fairly obvious from the name itself. i didn't know what they
    > meant by that when i first read it and i hence posted the question
    > here)


    I applaud your having read the documentation, that was an extremely
    good thing to do. However, if you do not understand the a piece of the
    documentation, the more appropriate path would have been to ask "Can
    someone please explain what this piece of documentation means?", rather
    than asking the question that was answered (albeit possibly not
    clearly) by the docs themselves. That would have much more quickly led
    you to a helpful answer.

    Out of curiousity, have you read the posting guidelines for this group?
    They discuss issues like this (mentioning what docs you've read before
    asking your question...).

    Paul Lalli
     
    Paul Lalli, Jun 23, 2005
    #6
  7. Perl Learner <> wrote:

    > i did look at
    >
    > perldoc -f system
    >
    > and i didn't understand what they meant by "backticks"



    Then your post should have been:

    What does "backticks" mean?

    But that wasn't the question that you asked...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 23, 2005
    #7
  8. Perl Learner

    Guest

    #!/usr/local/bin/perl
    #try qx if you don't want to use back ticks in UNIX like environments

    $a = qx#which gnuplot#;
    print $a ."\n";
     
    , Jun 23, 2005
    #8
  9. writes:

    > #!/usr/local/bin/perl
    > #try qx if you don't want to use back ticks in UNIX like environments
    >
    > $a = qx#which gnuplot#;
    > print $a ."\n";


    Gimme, will you *please* quote enough of the message you're replying to, for
    your own post to make sense? You've been asked this before - I'm beginning
    to get the idea that you're deliberately refusing to do so.

    Have a look at this URL:

    <http://groups-beta.google.com/support/bin/answer.py?answer=12348&topic=250>

    *Especially* the paragraph that begins with "Summarize what you're following
    up."

    sherm--
     
    Sherm Pendley, Jun 23, 2005
    #9
  10. Perl Learner

    Guest

    Thanks for the laugh Shrem.
     
    , Jun 24, 2005
    #10
  11. Perl Learner

    Perl Learner Guest

    Thanks for the replies folks.

    > Then your post should have been:
    >
    > What does "backticks" mean?
    >
    >But that wasn't the question that you asked...


    That wasn't the question I asked because when I first
    read that word, it sounded so faint that I didn't even think
    that the answer would lie in it.

    If I knew what it meant at first, I would have found it on the web
    myself. I later did exactly that to figure the answer out.

    Even if I had asked "what does backticks mean", I wouldn't
    be surprised if you'd give another one of those standard replies
    with that funny tone.

    I am asking questions here because I couldn't find them
    online/in the documentation (Sometimes I don't know the
    right terms to search for, I can only describe to myself what I want
    in plain English, and sometimes these documentation searches/
    google searches don't show the right results if you don't have the
    right terms to search for). And if I am unable to find what I need,
    I come here to post a question.

    >You need to learn to try searching the standard docs *before*
    >asking hundreds of people around the world to help you with
    >your problem.


    (apart from what I've said earlier... )
    Technically, (most of) everything could be found if you search
    hard enough. So basically I can reply to anyone here telling
    them to search the documentation and whatnot. But I wouldn't
    personally recommend myself using that strategy (or worse,
    using that tone)

    As for the rest of you guys, I really appreciate all the time and
    patience you have shown for my little perl brain so far. All your
    comments and suggestions are very valuable to me.
     
    Perl Learner, Jun 24, 2005
    #11
  12. Perl Learner <> wrote:


    Please provide an attribution when you quote someone.


    > Thanks for the replies folks.
    >
    >> Then your post should have been:
    >>
    >> What does "backticks" mean?
    >>
    >>But that wasn't the question that you asked...



    > Even if I had asked "what does backticks mean", I wouldn't
    > be surprised if you'd give another one of those standard replies
    > with that funny tone.



    > Technically, (most of) everything could be found if you search
    > hard enough. So basically I can reply to anyone here telling
    > them to search the documentation and whatnot. But I wouldn't
    > personally recommend myself using that strategy (or worse,
    > using that tone)



    I won't be helping you with an unacceptable tone in the future.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 25, 2005
    #12
  13. Perl Learner

    Perl Learner Guest

    Tad, I wasn't expecting any useful info from you either. So don't
    worry.
     
    Perl Learner, Jun 25, 2005
    #13
  14. "Perl Learner" <> writes:
    > Thanks for the replies folks.
    >
    > > Then your post should have been:
    > >
    > > What does "backticks" mean?
    > >
    > >But that wasn't the question that you asked...

    >
    > That wasn't the question I asked because when I first
    > read that word, it sounded so faint that I didn't even think
    > that the answer would lie in it.


    I'm curious, what does "faint" mean here? (My native language isn't
    English.)
     
    Arndt Jonasson, Jun 27, 2005
    #14
  15. Perl Learner

    Tintin Guest

    "Arndt Jonasson" <> wrote in message
    news:...
    >
    > "Perl Learner" <> writes:
    >> Thanks for the replies folks.
    >>
    >> > Then your post should have been:
    >> >
    >> > What does "backticks" mean?
    >> >
    >> >But that wasn't the question that you asked...

    >>
    >> That wasn't the question I asked because when I first
    >> read that word, it sounded so faint that I didn't even think
    >> that the answer would lie in it.

    >
    > I'm curious, what does "faint" mean here? (My native language isn't
    > English.)


    As an English speaker, it is a very unusual context to use "faint" in. I
    think the OP is using it in the sense of "not strong" or possibly "vague".
     
    Tintin, Jun 27, 2005
    #15
    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. chuck amadi
    Replies:
    1
    Views:
    504
    Larry Bates
    Jun 23, 2004
  2. alexLIGO
    Replies:
    4
    Views:
    1,311
    alexLIGO
    Oct 5, 2005
  3. grocery_stocker
    Replies:
    3
    Views:
    377
    Carl Banks
    Apr 10, 2009
  4. Peng Yu
    Replies:
    1
    Views:
    749
    Rominsky
    Oct 31, 2009
  5. PerlFAQ Server
    Replies:
    0
    Views:
    107
    PerlFAQ Server
    Mar 13, 2011
Loading...

Share This Page