2014-09-10 54 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; 
    } 
} 
相關問題