简体   繁体   English

如何使用Perl多次运行同一命令?

[英]How do I run the same command multiple times using Perl?

I have 2 commands that I need to run back to back 16 times for 2 sets of data. 我有2条命令需要对2组数据进行16次连续运行。 I have labeled the files used as file#a1_100.gen (set 1) and file#a2_100.gen (set 2). 我已将文件标记为file#a1_100.gen(设置1)和file#a2_100.gen(设置2)。 The 100 is then replaced by multiples of 100 upto 1600 (100,200,...,1000,...,1600). 然后将100替换为100的倍数,直到1600(100,200,...,1000,...,1600)。

Example 1: For first set 示例1:对于第一组

Command 1: perl myprogram1.pl file#a1.pos abc#a1.ref xyz#a1.ref file#a1_100.gen file#a1_100.out 命令1: perl myprogram1.pl file#a1.pos abc#a1.ref xyz#a1.ref file#a1_100.gen file#a1_100.out

Command 2: perl my program2.pl file#a1_100.out file#a1_100.out.long 命令2: perl my program2.pl file#a1_100.out file#a1_100.out.long

Example 2: For first set 示例2:对于第一组

Command 1: perl myprogram1.pl file#a1.pos abc#a1.ref xyz#a1.ref file#a1_200.gen file#a1_200.out 命令1: perl myprogram1.pl file#a1.pos abc#a1.ref xyz#a1.ref file#a1_200.gen file#a1_200.out

Command 2: perl my program2.pl file#a1_200.out file#a1_200.out.long 命令2: perl my program2.pl file#a1_200.out file#a1_200.out.long

These 2 commands are repeated 16 times for both set 1 and set 2. For set 2 the filename changes to File#a2... 这两个命令对于集合1和集合2重复16次。对于集合2,文件名更改为File#a2 ...

I need a command that will run this on its own by changing the filename for the 2 sets, running it 16 times for each set. 我需要一个命令,该命令将通过更改2套文件名来单独运行,每套文件运行16次。

Any help will be greatly appreciated! 任何帮助将不胜感激! Thanks! 谢谢!

This is probably most easily done with a shell script. 使用shell脚本可能最容易做到这一点。 As with Perl, TMTOWTDI — there's more than one way to do it. 与Perl和TMTOWTDI一样,有多种方法可以做到这一点。

for num in $(seq 1 16)
do
    perl myprogram1.pl file#a1.pos abc#a1.ref xyz#a1.ref file#a1_${num}00.gen file#a1_${num}00.out
    perl myprogram2.pl file#a1_${num}00.out file#a1_${num}00.out.long
done

(You could use {1..16} in place of $(seq 1 16) to generate the numbers. You might also note that the # characters in the file names discombobulate the SO Markdown system.) (您可以使用{1..16}代替$(seq 1 16)来生成数字。您可能还注意到文件名中的#字符破坏了SO Markdown系统。)

Or you could use: 或者您可以使用:

for num in $(seq 100 100 1600)
do
    perl myprogram1.pl file#a1.pos abc#a1.ref xyz#a1.ref file#a1_${num}.gen file#a1_${num}.out
    perl myprogram2.pl file#a1_${num}.out file#a1_${num}.out.long
done

(I don't think there's a {...} expansion for that.) (我认为没有{...}扩展。)

Or, better, you could use variables to hold values to avoid repetition: 或者,更好的是,您可以使用变量来保存值以避免重复:

POS="file#a1.pos"
ABC="abc#a1.ref"
XYZ="xyz#a1.ref"

for num in $(seq 100 100 1600)
do
    PFX="file#a1_${num}"
    GEN="${PFX}.gen"
    OUT="${PFX}.out"
    LONG="${OUT}.long"
    perl myprogram1.pl "${POS}" "${ABC}" "${XYZ}" "${GEN}" "${OUT}"
    perl myprogram2.pl "${OUT}" "${LONG}"
done

In this code, the braces around the parameter names are all optional; 在此代码中,参数名称周围的花括号都是可选的; in the first block of code, the braces around ${num} were mandatory, but optional in the second set. 在第一段代码中,在${num}左右的花括号是必需的,但在第二组花括号中则是可选的。 Enclosing names in double quotes is also optional here, but recommended. 在这里,将名称括在双引号中也是可选的,但建议这样做。

Or, if you must do it in Perl, then: 或者,如果必须在Perl中执行此操作,则:

use warnings;
use strict;

my $POS = "file#a1.ref";
my $ABC = "abc#a1.ref";
my $XYZ = "xyz#a1.ref";

for (my $num = 100; $num <= 1600; $num += 100)
{
    my $PFX = "file#a1_${num}";
    my $GEN = "${PFX}.gen";
    my $OUT = "${PFX}.out";
    my $LONG = "${OUT}.long";
    system("perl", "myprogram1.pl", "${POS}", "${ABC}", "${XYZ}", "${GEN}", "${OUT}");
    system("perl", "myprogram2.pl", "${OUT}", "${LONG}");
}

This is all pretty basic coding. 这都是非常基本的编码。 And you can guess that it didn't take me long to generate this from the last shell script. 您可以猜测,从上一个Shell脚本生成此文件并不需要很长时间。 Note the use of multiple separate strings instead on one long string in the system calls. 请注意,在system调用中,在多个长字符串上使用多个单独的字符串。 That avoids running a shell interpreter — Perl runs perl directly. 这就避免了运行shell解释器perl直接运行perl

You could use $^X instead of "perl" to ensure that you run the same Perl executable as ran the script shown. 您可以使用$^X代替"perl"以确保您运行与所示脚本相同的Perl可执行文件。 (If you have /usr/bin/perl on your PATH but you run $HOME/perl/v5.20.1/bin/perl thescript.pl , the difference might matter, but probably wouldn't.) (如果您在PATH上具有/usr/bin/perl ,但运行$HOME/perl/v5.20.1/bin/perl thescript.pl ,则差异可能很重要,但可能不会。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM