2017-10-26 12 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'を呼び出さなかった場合、あなたのプログラムは' exit'しないでしょう。 – ikegami

答えて

1

ここにあなたの問題がある - シェルがワイルドカードを展開し前に、あなたのperlスクリプトは、それらになります。追加

試してみてください。

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

そして、あなたはあなたのプログラムに渡さなっているものがわかります。

ワイルドカードを使用していると、複数のものと一致する可能性があります。そうであれば、プログラムは追加の引数を受け取ります。あなただけのプログラムはのみ 3.

1

は標準モジュールGetopt::StdFile::Findを見てみ受け入れ、あなたが使用して一部open/close文を保存することに注意します@ARGV化さlocal

#!/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. 
関連する問題