Perl脚本,同时通过一个文件给标准输入带参数运行C可执行文件?

StackOverflow https://stackoverflow.com/questions/1234287

  •  22-07-2019
  •  | 
  •  

我想运行和参数的 input.afa 可执行./runnable。标准输入到该可执行文件是通过文件的 finalfile 。我早前尝试使用bash脚本做同样的,但似乎并没有工作了。所以我想知道的Perl是否提供这样的功能。我知道我可以用它的参数使用反引号或系统()调用运行可执行文件。如何任何建议通过文件给标准输入。

_ 更新 _

正如我所说我写bash脚本用于相同。我不知道如何去用Perl这样做。在bash脚本我写的是:

#!/bin/bash

OUTFILE=outfile
(

while read line
do 

./runnable input.afa
echo $line


done<finalfile

) >$OUTFILE

在标准输入文件中的数据如下所示,其中每行对应于一个时间输入。因此,如果有10条线,然后该可执行应该运行10次。

__DATA__

2,9,2,9,10,0,38

2,9,2,10,11,0,0

2,9,2,11,12,0,0

2,9,2,12,13,0,0

2,9,2,13,0,1,4

2,9,2,13,3,2,2

2,9,2,12,14,1,2
有帮助吗?

解决方案

Perl代码:

$stdout_result = `exescript argument1 argument2 < stdinfile`;

在哪里stdinfile拥有你想要的数据通过标准输入进行传递。


修改

在巧妙的方法是打开stdinfile,通过选择配合它到标准输入,然后执行多次。最简单的方法是把你想在一个临时文件通过数据。

示例:

open $fh, "<", "datafile" or die($!);
@data = <$fh>; #sucks all the lines in datafile into the array @data
close $fh;

foreach $datum (@data) #foreach singluar datum in the array
{
    #create a temp file
    open $fh, ">", "tempfile" or die($!);
    print $fh $datum;
    close $fh;

    $result = `exe arg1 arg2 arg3 < tempfile`; #run the command. Presumably you'd want to store it somewhere as well...

    #store $result
}

unlink("tempfile"); #remove the tempfile

其他提示

如果我理解正确你的问题,那么你就可能寻找的东西是这样的:

# The command to run.
my $command = "./runnable input.afa";

# $command will be run for each line in $command_stdin
my $command_stdin = "finalfile";

# Open the file pointed to by $command_stdin
open my $inputfh, '<', $command_stdin or die "$command_input: $!";

# For each line
while (my $input = <$inputfh>) {
    chomp($input); # optional, removes line separator

    # Run the command that is pointed to by $command,
    # and open $write_stdin as the write end of the command's
    # stdin.
    open my $write_stdin, '|-', $command or die "$command: $!";

    # Write the arguments to the command's stdin.
    print $write_stdin $input;
}

关于在文档打开命令更多信息。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top