0

我有一个应用程序可执行文件,它使用不同的参数运行以产生不同的输出。我想从脚本的命令行参数中为此提供一些参数,而其他参数将是脚本的本地参数。用法:

./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`;
}

我对上面的代码做了一些固有的错误,但一直无法弄清楚:-(

4

2 回答 2

5
# 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";
    }
}
于 2010-02-21T16:00:59.107 回答
3

for 循环丢失了$

outputtype 数组缺少$itr索引的 a

可能还有更多——我没有测试过。这是显而易见的东西。

看起来您可能来自像 C 这样的语言,其中变量可以是像“ i”这样的裸词。perl 中的变量总是以$for scalar、@for list、%for hash 开头。

于 2010-02-21T15:52:56.647 回答