Perl script per eseguire un eseguibile C con un argomento mentre fornisce l'input standard attraverso un file?

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

  •  22-07-2019
  •  | 
  •  

Domanda

Voglio eseguire ed eseguire ./runnable sull'argomento input.afa . L'input standard per questo eseguibile è attraverso un file finalfile . Prima stavo provando a fare lo stesso usando uno script bash, ma questo non sembra funzionare. Quindi mi chiedevo se Perl fornisse tale funzionalità. So di poter eseguire l'eseguibile con il suo argomento usando backtick o system () call. Eventuali suggerimenti su come fornire input standard tramite file.

_ AGGIORNAMENTO _

Come ho detto, avevo scritto una sceneggiatura bash per lo stesso. Non sono sicuro di come farlo in Perl. Lo script bash che ho scritto era:

#!/bin/bash

OUTFILE=outfile
(

while read line
do 

./runnable input.afa
echo $line


done<finalfile

) >$OUTFILE

I dati nel file di input standard sono i seguenti, in cui ogni riga corrisponde a un input temporale. Quindi, se ci sono 10 righe, l'eseguibile dovrebbe essere eseguito 10 volte.

__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
È stato utile?

Soluzione

Codice Perl:

$stdout_result = `exescript argument1 argument2 < stdinfile`;

Dove stdinfile contiene i dati che si desidera passare attraverso stdin.


modifica

Il metodo intelligente sarebbe quello di aprire stdinfile, legarlo tramite select a stdin e quindi eseguirlo ripetutamente. Il metodo semplice sarebbe quello di inserire i dati che si desidera passare in un file temporaneo.

Esempio:

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

Altri suggerimenti

Se ho capito bene la tua domanda, forse stai cercando qualcosa del genere:

# 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;
}

Ulteriori informazioni sull'apertura dei comandi nella documentazione .

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top