2014-09-10 55 views
0

他们希望我创建一个供稿目录,该目录是来自 autosys的所有作业的表格,然后是作业所依赖的所有信息和出口文件。没有这样的文件或目录 - Perl死文件关闭程序

的想法是,而不是在凌晨3:00打电话给我的老板了在任务失败时, 水平可以失败的作业匹配到一个文件,反之亦然,并希望 解决问题。

该脚本会在autosys配置文件,取出executable线,子吧, 打开了文件,然后从正则表达式的路径线。它工作正常,但它无法打开文件时会排出 。

我收到此错误。

dead file handle No such file or directory at ./slurp_autosys_config.justexec2 line 10, <$autosys_fh> line 352. 

#!/usr/bin/perl 

use strict; 
use warnings; 

open my $autosys_fh, '<', "/data/autosys-us.cfg" or die "can't open file $!"; 

while (my $cfg_line = <$autosys_fh>) { 

    if ($cfg_line =~ /executable/) { 

     my $cut_cfg_line = substr "$cfg_line", 13; 

     if ($cut_cfg_line =~ /(\/\S*\.[sh,pl,ksh])/) { 

     chomp($cut_cfg_line); 
     open my $fh_cut_cfg, '<', $cut_cfg_line or die "dead file handle $!"; 

     while (my $path = <$fh_cut_cfg>) { 
      if ($path =~ /(\"\/\S*)\"/) { 
       print "$cut_cfg_line ---> $path"; 
       sleep 1; 
      } 
     } 
     } 
    } 
} 

这就是结果。这是我想要的

[email protected]:~/walt $ ./slurp_autosys_config.justexec2 
/data/adp/UBSS/adploggerUBSS.sh.new ---> DIR="/data/scripts" 
/data/evtUSBUCgediLoader.sh ---> $out="/data/px"; 
/data/evtUSBUCgediLoader.sh ---> $in="/data/infile"; 
/data/cboe_xml_hack_check.pl --->  my $xml="/data/C4symbolgroupdefconfig.xml"; 
/data/cboe_xml_hack_check.pl --->  my $xml="/data/qedefconfig.xml"; 
/data/WebDownloads/getCBOE_BC_csv.pl ---> my $reallysave = "/data/BCALLCLASSES.csv"; 
/data/WebDownloads/getCBOE_BC_csv.pl ---> my $save = "/data/CBOE_BC.csv"; 
/data/WebDownloads/getCBOE_BC_csv.pl ---> my $output_file="/data/CBOE_BC.csv"; 
/data/WebDownloads/getCBOE_BC_csv.pl ---> system("/data/CBOE_BC_Checker.pl"); 
/data/dbscripts/getCBOE_BC.pl --->  my $bc_file = "/data/CBOE_BC.csv"; 
/data/dbscripts/getCBOE_BC.pl --->  my $bc_file = "/data/C2_BC.csv"; 
/data/dbscripts/getCBOE_BC.pl --->  my $bc_file = "/data/CBOE_BC.csv"; 
/data/dbscripts/getCBOE_BC.pl --->  my $bc_file = "/data/C2_BC.csv"; 
/data/WebDownloads/CBOE_restricted.pl ---> $compliance_dir="/data/compliance"; 
/data/WebDownloads/CBOE_restricted.pl ---> $puc_file="/data/Restricted.csv"; 
/data/WebDownloads/CBOE_restricted.pl ---> $final_file="/data/Restricted.csv"; 
/data/WebDownloads/CBOE_restricted.pl ---> #$second_file="/data/ISERestricted.csv"; 
/data/checkClosingPrices.pl ---> $pxdir="/db/irdb/px"; 

dead file handle No such file or directory at ./slurp_autosys_config.justexec2 line 10, <$autosys_fh> line 352. 

当我检查这个文件 - 文件352 - 我无法打开文件。没有什么。

  • 如何让程序在有空文件的情况下运行?
  • 如何跟踪空文件?

#!/usr/bin/perl 

use strict; 
use warnings; 

open my $autosys_fh, '<', "/data/autosys-us.cfg" or die "can't open file $!"; 

while (my $cfg_line = <$autosys_fh>) { 
    if ($cfg_line =~ /executable/) { 
     print "$. $cfg_line"; 
    } 
} 

这是排查结果 - 这是一条走不通的文件。

[email protected]:~/walt $ ./cont-executable | head -22 | tail -2 
352 executable = /data/checkClosingPrices.v2.pl 
365 executable = /data/cboe_quote_rate_check.sh 
[email protected]:~/walt $ 
[email protected]:~/walt $ cat /data/checkClosingPrices.v2.pl 
cat: /data/checkClosingPrices.v2.pl: No such file or directory 
[email protected]:~/walt $i 
+1

为什么不使用'next'跳过该行而不是死亡? – Barmar 2014-09-10 17:15:29

+1

>如果有空文件,我该如何运行程序? 一个建议是,你不会*死在它上面。 '打开我的$ fh_cut_cfg,'<',$ cut_cfg_line或死“死文件句柄$!”;' – Axeman 2014-09-10 17:15:50

+0

我看不到空行可能会导致此问题。如果该行为空,则不会匹配'/ executable /' – Barmar 2014-09-10 17:16:47

回答

2

这应该适合你。它修复了第一个正则表达式中的错误([sh,pl,ksh]仅匹配单个字符,与[,hklps]相同),并警告并继续,如果有任何文件无法打开。

use strict; 
use warnings; 

open my $autosys_fh, '<', '/data/autosys-us.cfg' or die "Can't open autosys config file: $!"; 

while (<$autosys_fh>) { 

    next unless /executable/; 

    chomp; 
    my $cut = substr $_, 13; 
    next unless $cut =~ m{/\S* \. (?: sh | pl | ksh) }x; 

    open my $cut_fh, '<', $cut or do { 
     warn qq{Can't open file "$cut": $!}; 
     next; 
    }; 

    while (<$cut_fh>) { 
     next unless m{ "/ \S* " }x; 
     my $path = $_; 
     print "$cut ---> $path"; 
     sleep 1; 
    } 
} 
相关问题