برل لتشغيل قابل للتنفيذ C مع وسيطة في حين يعطي الإدخال القياسي من خلال ملف؟

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

  •  22-07-2019
  •  | 
  •  

سؤال

وأريد أن أشغل و./runnable للتنفيذ على حجة <م> input.afa . الإدخال القياسي لهذا الملف التنفيذي هو عبارة عن ملف <م> finalfile . كنت في وقت سابق تحاول أن تفعل الشيء نفسه باستخدام برنامج نصي باش، ولكن هذا لا يبدو أن ينجح في مسعاه. لذلك كنت أتساءل عما إذا كان بيرل توفر هذه الوظيفة. أعرف أنني أستطيع أن تشغيل قابل للتنفيذ مع وذلك باستخدام backticks حجة أو الاتصال بالرقم () النظام. أي اقتراحات بشأن كيفية إعطاء الإدخال القياسي من خلال الملف.

_ <م> UPDATE _

وكما قلت كنت قد كتبت السيناريو باش لنفسه. أنا غير متأكد من كيفية التوجه نحو القيام بذلك في بيرل. السيناريو باش كتبت هو:

#!/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
هل كانت مفيدة؟

المحلول

وبيرل مدونة:

$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