ファイルを介して標準入力を与えながら、引数を指定してC実行可能ファイルを実行するPerlスクリプト?
質問
引数 input.afa で実行および実行可能な ./ 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
標準入力ファイルのデータは次のとおりです。各行は1回の入力に対応しています。 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には、stdinに渡すデータが格納されています。
編集
巧妙な方法は、stdinfileを開き、selectを介して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;
}
ドキュメントのコマンドを開くことに関する詳細情報。
所属していません StackOverflow