Perl应用(1):通过shell脚本分割实现多任务投递

#!/usr/bin/perl -w

use Getopt::Long;
use File::Path;
use File::Basename;
use Cwd 'abs_path';
my ($shell,$num,$shdir,$threads,$sign,$run);

GetOptions(
  "i|in-file=s"  =>\$shell,
  "n|num=i" =>\$num,
  "s|sign=s" =>\$sign,  
  "o|outdir=s"  =>\$shdir,
  "r|run=s"  =>\$run,
  );


($shell && -s $shell)||die "Function: split shell script and qsub job 
Usage : $0 -i <.sh> [-nstor]
Version : v1 at 2016-12-20; 
Contact: j_h_yu\@163.com
    -i  <str>   input shell script
    -n  <int>   the tasks num of each sub shell after splitting,default=1
    -s  <str>   set the sign split by,default='\\n\\n'
    -o  <str>   set outdir, default='./shellname.qsub.rundate'
    -r          if(y/Y) or not(n/N) run the scripts,default='y' 
";

my $rundate=`date +'%m%d%H%M'`;
chomp $rundate;

$num    ||="1";
$shdir  ||="./$shell.qsub.$rundate";
$sign   ||="\n\n";
$run    ||='y';

$shdir=abs_path($shdir);
$shdir|| `mkdir -p $shdir`;
mkpath $shdir unless ( -d $shdir);

$/=$sign;
open IN,$shell or die "ERROR: open $shell: $!";
my $name=basename($shell);
$name=~s/\.sh$//;
$name.=".sub";
my $flag=1;
my $n=0;
my $qsub='';
my %sh;
open OUT, ">$shdir/$name.$flag.sh" || die "ERROR: write $name.$flag.sh: $!";
$sh{"$name\.$flag\.sh"}=1;
while(<IN>){
    chomp;
    $n++;
    if($n > $num){
        $n=1;
        $flag++;    
        close OUT;
        open OUT, ">$shdir/$name.$flag.sh" || die "ERROR: write $name.$flag.sh: $!"; 
        $sh{"$name\.$flag\.sh"}=1;      
    };
    print OUT "$_\n\n";
}
close IN;
close OUT;
$/="\n";
for(sort (keys %sh)){$qsub.="& sh $_ &>$_\.log"};
$qsub=~s/^\&//;
system "cd $shdir;$qsub\n" if($run=~/y/i);
    
    原文作者:魚晨光
    原文地址: https://www.jianshu.com/p/fa1e1dc257de
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞