2013-10-30 36 views
0

我想用perl来运行一系列的系统命令。我写了for循环如何用perl建立流水线

#!/usr/bin/perl 
use warnings; 
use strict; 
foreach my $ichr (1 .. 21){ 
    system "bedtools intersect -wb -a genes.bed -b chr$ichr.bed > Counts_chr$ichr.txt"; 
} 

我不确定循环是否会提交每个命令到系统并启动下一个?我希望在开始下一个之前完成一个。 (例如,完成chr1,然后启动chr2,完成chr2,然后启动chr3等)

如何确保一个命令完成,然后启动下一个命令?

+0

当你尝试过时发生了什么? –

回答

3
perldoc -f system 
  Does exactly the same thing as "exec LIST", except that a fork 
      is done first and the parent process waits for the child 
      process to exit. 

所以system命令将等待启动的进程来完成它继续下一个循环迭代之前。

+0

太棒了。我有一个后续问题。这仅适用于for循环中的命令吗?如果我有两个系统命令不在for循环中,我需要等待第一个完成,然后运行第二个(基本上我想构建一个管道,并且需要第一个系统命令才能在perl提交之前完成下一个系统命令)。 – user1007742

+0

引用给你的文档根本没有提到for循环。因此,假定所描述的行为与循环无关是合理的:-) –

+0

当系统调用启动运行子进程时,它暂停perl进程,并且在子进程退出时它将继续。所以系统命令将被逐个处理。 –