I want to have output of Windows command-line program (say, powercfg -l
) written into a file which is created using Perl and then read the file line by line in a for loop and assign it to a string.
相关问题
- $ENV{$variable} in perl
- xcopy include folder
- Batch file if string starts by
- Jenkins - cmd is not recognized
- Is it possible to pass command-line arguments to @
相关文章
- Running a perl script on windows without extension
- Comparing speed of non-matching regexp
- Compile and build with single command line Java (L
- Can NOT List directory including space using Perl
- Extracting columns from text file using Perl one-l
- Lazy (ungreedy) matching multiple groups using reg
- How do I tell DBD::mysql where mysql.sock is?
- How to update command line output?
You have some good answers already. In addition, if you just want to process a command's output and don't need to send that output directly to a file, you can establish a pipe between the command and your Perl script.
To expand on Sinan's excellent answer and to more explicitly answer your question:
NOTE: backticks `` tell Perl to execute a command and retrieve its output:
Your output (on my system) would be:
Perl makes it easy!
is the recommended way. If you don't mind spawning a subshell,
will work, too. And if you want the results in a string:
Try using > operator to forward the output to a file, like:
And then open output.txt and process it.
There is no need to first save the output of the command in a file:
See
qx//
in Quote-Like Operators.However, if you do want to first save the output in a file, then you can use:
See perldoc -f system.
Since the OP is running
powercfg
, s/he are probably capturing the ouput of the external script, so s/he probably won't find this answer terribly useful. This post is primarily is written for other people who find the answers here by searching.This answer describes several ways to start command that will run in the background without blocking further execution of your script.
Take a look at the perlport entry for
system
. You can usesystem( 1, 'command line to run');
to spawn a child process and continue your script.This is really very handy, but there is one serious caveat that is not documented. If you start more 64 processes in one execution of the script, your attempts to run additional programs will fail.
I have verified this to be the case with Windows XP and ActivePerl 5.6 and 5.8. I have not tested this with Vista or with Stawberry Perl, or any version of 5.10.
Here's a one liner you can use to test your perl for this problem:
If the problem exists on your system, and you will be starting many programs, you can use the Win32::Process module to manage your application startup.
Here's an example of using
Win32::Process
: