2017-10-26 5 views
0

현재 2 또는 3 개의 명령 줄 인수를 사용하는 프로그램이 있습니다. find.pl [i] <perlRegexPattern> <listOfFiles>. 이제 find.pl proj1 Data 또는 find.pl -i proj1 Data을 전달하면 프로그램이 if 문을 통과하지만 내 프로그램이 받아들이려고 find.pl -i ".*proj1.*" DataA/* 같은 것을 받아 들일 때 제 프로그램은 제 2 if 문을 지나치지 않게합니다. 필자는 이러한 매개 변수를 매개 변수로 전달한 다음 내 프로그램에서 사용하는 방법을 잘 모릅니다. 여기Perl이 명령 줄 인수를 전달합니다.

#!/usr/bin/perl -w 
if(@ARGV < 2){ 
    print "\n2) Usage: find.pl [-i] <perlRegexPattern> <listOfFiles>\n\n"; 
    exit; 
} 
if(@ARGV > 3){ 
    print "\n3) Usage: find.pl [i] <perlRegexPattern> <listOfFiles>\n\n"; 
    exit; 
} 
if(@ARGV == 3 && $ARGV[0] ne "-i"){ 
    die "\n4) Usage: find.pl [i] <perlRegexPattern> <listOfFiles>\n\n"; 
} 

if ($#ARGV eq 1){ 
    print "normal case\n"; 
    my ($pattern, $filelist) = @ARGV; 
    print "$pattern $filelist\n"; 
    opendir(DIR , $filelist) or die "\nCannot open directory $filelist\n\n"; 

    while (($fp = readdir(DIR))){ 
     if($fp =~ m/$pattern/){ 
      print "$fp\n"; 
     } 
     elsif($fp =~ m/.*\.txt/){ 
      print "$fp is a text file\n"; #Still working on this part 
      open(FILE, '<' , $fp) or die ("\nCould not open file $fp\n\n"); 
      while(<FILE>){ 
       if($_ =~ m/$pattern/){ 
        print "$fp : $_ : line pattern match\n"; 
        last; 
       } 
      } 
      close(FILE); 
     } 
    } 
} 
else{ 
    print "-i case\n"; 
    my ($pattern, $filelist) = @ARGV[1 .. 2]; 
    print "$pattern $filelist\n"; 
    #TODO 
} 
+0

둘 이상의 파일이 전달 될 때'exit'를 호출합니다. ('@ARGV> 3') ... – ikegami

+0

@ikegami 나는 당신이 의미하는 것을 얻지 못합니다. –

+2

'exit'를 호출하지 않았다면 프로그램이 종료되지 않을 수도 있습니다. – ikegami

답변

1

귀하의 문제는 - 쉘은 전에 펄 스크립트가 그들에게 얻는 와일드 카드를 확장합니다. 추가

시도 :

use Data::Dumper; 
print Dumper \@ARGV; 

그리고 당신은 당신의 프로그램에 전달하기 것만 볼 수 있습니다.

와일드 카드를 사용하고 있다면 두 가지 이상이 일치 할 가능성이 높습니다. 그렇다면 프로그램에 추가 인수가 전송됩니다. 만 프로그램이 3.

1

은 표준 모듈 Getopt::StdFile::Find에서 봐 받아들이고 사용하여 일부 open/close 문을 절약 할 수 있습니다합니다 @ARGV화된 local A :

#!/usr/bin/perl -w 

use strict; 
use Getopt::Std; 
use File::Find; 

## Parse option(s) 
getopts 'i', \my %opt; 

## Show help 
die <<usage unless @ARGV > 1; 
Usage: 
    $0 [options] pattern searchpaths... 

Options: 
    -i Case insensitive search 
usage 

## Compile regex 
my $re = shift; 
if ($opt{i}) { $re = qr($re)i } 
else   { $re = qr($re) } 

## Walk searchpaths 
find (sub 
    { 
    ## Filename matches the regex 
    if (/$re/) 
    { 
     printf "match name: %s\n", $File::Find::name 
    } 
    ## File is regular, name ends with .txt, and... 
    elsif (-f and /\.txt$/) 
    { 
     local @ARGV = $_; 
     while (<>) 
     { 
     ## ... a line in the file matches the regex. 
     chomp; 
     /$re/ 
      and printf "match content: %s: %s\n", $File::Find::name, $_ 
      and last; 
     } 
    } 
    }, 
    @ARGV); 

예 :

find.pl -i ^fa /usr/share/doc/lighttpd 

출력 :

match name: /usr/share/doc/lighttpd/fastcgi-state.txt 
match name: /usr/share/doc/lighttpd/fastcgi.txt 
match content: /usr/share/doc/lighttpd/security.txt: FastCGI 
match content: /usr/share/doc/lighttpd/accesslog.txt: fastcgi + chroot 
match content: /usr/share/doc/lighttpd/performance.txt: failed'. This is very rare and might only occur in test setups.