windows 通过管道将参数传递给系统一个接一个的外部命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4028405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:29:16  来源:igfitidea点击:

Pass arguments through pipe to external command with system one after another

windowsperl

提问by abishek mann

I am trying to open a external command from Perl using system call. I am working on Windows. How can I pass arguments to it one after another?

我正在尝试使用系统调用从 Perl 打开外部命令。我在 Windows 上工作。我怎样才能一个接一个地向它传递参数?

For example:

例如:

system("ex1.exe","arg1",arg2",....); 

Here ex1.exeis external command and i would like it to process arg1 first and then arg2 and so on...

ex1.exe是外部命令,我希望它先处理 arg1,然后再处理 arg2,依此类推...

I would appreciate for your reply,

我很感激你的回复,

回答by Pedro Silva

Use a pipe open:

使用管道打开

use strict; 
use warnings;

{
    local ++$|;

    open my $EX1_PIPE, '|-', 'ex1.exe' 
        or die $!;

    print $EX1_PIPE "$_\n"
        for qw/arg1 arg2 arg3/;

    close $EX1_PIPE or die $!;
}

I'm assuming you want to pipe data to ex1.exe's STDIN; for example, if ex1.exeis the following perl script:

我假设您想将数据通过管道传输到ex1.exe's STDIN;例如,如果ex1.exe是以下 perl 脚本:

print while <>;

Then if you run the above code your output should be:

然后如果你运行上面的代码,你的输出应该是:

arg1
arg2
arg3

回答by mfollett

Are you trying to execute ex1.exe once for each argument? Something similar to:

您是否尝试为每个参数执行一次 ex1.exe?类似于:

> ex1.exe arg1
> ex1.exe arg2
> ex1.exe arg3

If so, you would do:

如果是这样,你会这样做:

for my $arg (@args)
{
   system( 'ex1.exe', $arg);
}