在通过文件提供标准输入的同时运行带参数的C可执行文件的Perl脚本?
我想在argument input.afa上运行并执行在通过文件提供标准输入的同时运行带参数的C可执行文件的Perl脚本?,perl,stdin,Perl,Stdin,我想在argument input.afa上运行并执行/runnable。此可执行文件的标准输入是通过文件finalfile。早些时候,我曾尝试使用bash脚本执行同样的操作,但似乎没有成功。所以我想知道Perl是否提供了这样的功能。我知道我可以使用backticks或system()调用运行带有参数的可执行文件。关于如何通过文件提供标准输入的任何建议 _更新_ 正如我所说,我已经为此编写了一个bash脚本。我不知道如何在Perl中实现它。我写的bash脚本是: #!/bin/bash OUT
/runnable
。此可执行文件的标准输入是通过文件finalfile。早些时候,我曾尝试使用bash脚本执行同样的操作,但似乎没有成功。所以我想知道Perl是否提供了这样的功能。我知道我可以使用backticks或system()调用运行带有参数的可执行文件。关于如何通过文件提供标准输入的任何建议
_更新_
正如我所说,我已经为此编写了一个bash脚本。我不知道如何在Perl中实现它。我写的bash脚本是:
#!/bin/bash
OUTFILE=outfile
(
while read line
do
./runnable input.afa
echo $line
done<finalfile
) >$OUTFILE
如果我正确理解了你的问题,那么你可能在寻找这样的东西:
# 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;
}
#要运行的命令。
my$command=“./runnable input.afa”;
#$command\u stdin中的每一行都将运行$command
我的$command\u stdin=“finalfile”;
#打开$command\u stdin指向的文件
打开我的$inputfh,“Perl代码:
$stdout_result = `exescript argument1 argument2 < stdinfile`;
$stdout_result=`exescript argument1 argument2
其中stdinfile保存要通过stdin传递的数据
编辑
聪明的方法是打开stdinfile,通过select将其绑定到stdin,然后重复执行。简单的方法是将要传递的数据放入临时文件中
例如:
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
打开$fh、“、”tempfile”或die($!);
打印$fh$数据;
收盘价$fh;
$result=`exe arg1 arg2 arg3
您是否在寻找类似于/runnable input.afa<*final_file*
?@RC:我不确定是否正确理解了您的评论。但是在运行你写的东西时,我收到一条错误消息“Segmentation Fault”。你为什么不举一些你已经尝试过的例子呢?@Brad:我没有相同的Perl脚本。我已经写了一个bash脚本,我已经添加了这个脚本。@Inshalla:你能给我添加一些注释让我更好地理解它吗。在这个例子中,backticks``通过shell执行命令,它处理输入重定向。@Paul:RC也建议这样做。但是,在运行它时,我得到了一个分段错误,这不应该是perl的错。您的C可执行文件是否正确处理参数。@Paul:那么您的建议是可行的,但问题与我在使用bash脚本时遇到的问题类似。它只运行一次,即一次迭代。最终文件包含10行,因此迭代次数应为10,而不是1。任何关于如何解决这个问题的建议。@shubster:谢谢你的接受。Inshalla采用了聪明的方法,仅供参考。:-)
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