Frage

Ich habe eine ausführbare Anwendung, die mit unterschiedlichen Parametern ausgeführt wird zu unterschiedlichen Ergebnissen führen. Ich möchte einige Parameter dieses von den Befehlszeilenparameter des Skripts und andere geben, um das Skript lokal sein. Verbrauch:

./dump-output.pl <version> <folder-name> <output-file>


my $version = $ARGV[0];
my $foldername = $ARGV[1];
my $outputfile = $ARGV[2];
my $mkdir_cmd = "mkdir -p ~/$foldername";

# There are 6 types of outputs, which can be created:
# 'a', 'b', 'c', 'd', 'e' or 'f'
my @outputtype = ('a', 'b', 'c', 'd', 'e', 'f');

my $mkdir_out = `$mkdir_cmd`;

for( $itr=0; itr<=5; itr++ ) {
    $my_cmd = "./my_app -v $version -t $outputtype[itr] -f $outputfile > ~/$foldername/$outputtype.out"
    $my_out = `$my_cmd`;
}

Ich bin etwas grundsätzlich falsch mit dem obigen Code zu tun, aber nicht in der Lage, es herauszufinden: - (

War es hilfreich?

Lösung

# Always include these at the top of your programs.
# It will help you find bugs like the ones you had.
use strict;
use warnings;

# You can get all arguments in one shot.
my ($version, $foldername, $outputfile) = @ARGV;

# A flag so we can test our script. When
# everything looks good, set it to 1.
my $RUN = 0;

my $mkdir_cmd = "mkdir -p ~/$foldername";
my $mkdir_out = run_it($mkdir_cmd);

# Word quoting with qw().
my @outputtype = qw(a b c d e f);

# If you already have a list, just iterate over it --
# no need to manually manage the array subscripts yourself.
for my $type (@outputtype) {
    my $my_cmd = "./my_app -v $version -t $type -f $outputfile > ~/$foldername/$type.out";
    my $my_out = run_it($my_cmd);
}

# Our function that will either run or simply print
# calls to system commands.
sub run_it {
    my $cmd = shift;
    if ($RUN){
        my $output = `$cmd`;
        return $output;
    }
    else {
        print $cmd, "\n";
    }
}

Andere Tipps

Die for-Schleife hat fehlende $

Der Output Array fehlt einen $itr für den Index

Es kann mehr sein - habe ich nicht getestet. Dies ist die offensichtlichen Sachen.

Es sieht aus wie vielleicht haben Sie von einer Sprache wie C kommen, wo eine Variable eine Bareword wie „i“ sein kann. Variablen in Perl immer mit $ für skalare, @ für Liste, % für Hash starten.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top