Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Perl System('') question

by renegadex (Beadle)
on Nov 29, 2008 at 02:24 UTC ( #726709=perlquestion: print w/replies, xml ) Need Help??

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

hi! how do I get the output of commands returned by the system('') command? For example..
system('eject -n')
this should return something like this...
eject: device is `/dev/sr0'
I wanted to get the output eject: device is `/dev/sr0' and place it inside a variable.. how do I do that?
Mabuhay Civil Engineers! :D

Replies are listed 'Best First'.
Re: Perl System('') question
by almut (Canon) on Nov 29, 2008 at 02:28 UTC

    Use backticks instead of system:

    my $out = `eject -n`;

    (in case eject is writing to stdout, that is, otherwise redirect stderr to stdout (`eject -n 2>&1`)...)

Re: Perl System('') question
by oko1 (Deacon) on Nov 29, 2008 at 03:14 UTC

    The advice given by the other folks here is good, but I'd like to suggest using an alternate operator provided by Perl for just this purpose. Backticks are easily misread as single-quotes, so using this improves readability:

    my $output = qx#/usr/bin/eject -n#;

    See perldoc perlop for more info about 'qx'.


    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf
      Hi, i would like give some more information on this, if the output is more then one line, then use array to get the output.
      my @output = qx(/usr/bin/eject -n);

      If you want to use system function then you can redirect your output to a file and read the file content. it might useful, when you running a program as a daemon.
      The backtick and qx are same, you can use the following option to check your code.
      perl -MO=Deparse <scriptname>.
        What would be the purpose of B::Deparse output? Just use the -c option to check syntax: perl -c file.
Re: Perl System('') question
by Lawliet (Curate) on Nov 29, 2008 at 02:30 UTC

    Use backquotes (or graves, or backticks, or whatever you want to call them). They are usually located to the left of the '1' key on your keyboard. They are on the same key as the tilde: ~

    my $output = `eject -n`;

    I'm so adjective, I verb nouns!

    chomp; # nom nom nom

Re: Perl System('') question
by Skeeve (Parson) on Nov 29, 2008 at 08:42 UTC

    And another alternative is to use open:

    my $result; # first open or die open my $eject, '-|', 'eject -n' or die "Can't eject: $!\n"; # Then read in lineby line while (<$eject>) { next unless /^device is/; # until the result is found $result= $_; last; } # done close $eject;

    Especially when filtering output, I prefer this method because you can loop over the output line by line and react as soon as you found your searched input.


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
      Hmmm... What does this do to a program that's still running when you "last" out of the loop? Like, say you're running a find, and want to kill the find as soon as it spots the thing it's looking for?
      for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";
        close $eject; will wait for the child to end (after closing pipes to the child?) If you wish to save it from doing extra work, you could ask it to end by sending a TERM signal to it before calling close. The return value of open '-|' is the child's PID.
        close $eject; will wait for the child to end (after closing pipes to the child?) If you wish to save it from doing extra work, you could ask it to end by sending a TERM signal to it before calling close. The return value of open '-|' is the child's PID.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://726709]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2022-05-21 06:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (76 votes). Check out past polls.

    Notices?