Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Output of a command

by Niel (Initiate)
on Jul 30, 2008 at 04:23 UTC ( #701015=perlquestion: print w/replies, xml ) Need Help??

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

The output of any our product command (like ebs --version) cannot be stored in a variable/file. (But same works fine if it’s a system command like date, ls etc on linux m/c). Is there way out to store this output into any file/variable? Below is the code in perl and it worked if the $command = ls but doesn’t work if the $command = “ebs --version”. Can you please give some input on this? Script is as follows…
$command = ls;
my $test = "/usr/anil/scripts/anil1.txt";
open (STDOUT, "> $test");
$t = system("$command");

Replies are listed 'Best First'.
Re: Output of a command
by ysth (Canon) on Jul 30, 2008 at 05:10 UTC
      YES you are right. Thanks on this!!!!! The output is going to STDERR.
      Now my question is, should it go to STDERR when its not an error? this is just get the information of the product!!!!
        You could be using IPC::Open3 , it will get output from STDOUT,STDERR .
        Some examples (*nix) if you only need one stream of the commands output:
        $command = "curl -D- http://www.perlmonks.org"; # mixes stderr and st +dout # stdout only, no stderr noise $stdout_only = `$command 2>/dev/null`; # stderr only, no stdout noise $stderr_only = `$command 2>&1 1>/dev/null`; # both mixed up (usually not a good idea!) # Aliens suggestion to use open3 is better in this case. $brundlefly = `$command 2>&1`;
Re: Output of a command
by Lawliet (Curate) on Jul 30, 2008 at 04:30 UTC

    Use the grave character.

    $t = `$command`;

    Grave can be found under the tilde (to the left of '1' on standard keyboards).

    <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>
      I have changed the $command to `$command`. Do you mean the below one??
      $command = ls;
      my $test = "/usr/anil/scripts/anil1.txt";
      open (STDOUT, "> $test");
      $t = `$command` If YES, then it didn't work!!!
Re: Output of a command
by toolic (Bishop) on Jul 30, 2008 at 13:08 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2021-04-15 14:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?