2013-05-09 2 views
2

디렉토리 및 .tar.gz 파일을 찾는 스크립트가 있습니다. 어떤 이유 (즉, 나는 당신이 나를 도울 수 바라고 무엇) 내Perl 디렉토리 찾기가 작동하지 않습니까?

if (-d $file) 

이 디렉토리의 것을 반환하지 않습니다!

내 스크립트 :

# specify the directory where you want to start the search 
my $directory = $ARGV[0]; 
my $directoryCount = 0; 

# Calling the Subroutine, which searches the File 
readDirectory($directory); 

sub readDirectory 
{ 
    my $directory = shift; 
    my $searchfile = shift; 
     my @directories; 
     my $tarOuput; 

    # a little bit output, in which directory the script 
    # is searching at the moment (the following line is not necessary) 
    print "Searching in $directory\n\n"; 

    # Open and close the directory 
    opendir(DIR, $directory) or die("ERROR: Couldn't open specified directory $!"); 
    my @files = readdir DIR; 
    closedir DIR; 

    shift(@files); 
    shift(@files); 

    print "------------------------------------------------------------------------ \n\n"; 

    foreach my $currentFile (@files) 
    { 
     print "Current File:", $currentFile, "\n\n"; 
     if (-d $currentFile) #THIS DOESN'T WORK?!?!?!?!? 
     { 
         print "Directory: ", $currentFile, "\n\n"; 
         #push (@directories, $currentFile); 
         #print "Found new directory:  $directories[$directoryCount]\n\nCurrent number = $directoryCount\n\n"; 
         #$directoryCount++; 
         #print "Directories: ", @directories, "\n\n"; 
         #next; 
         # The Subroutine is calling hisself with the new parameters 
         #readDirectory($currentFile); #recursive call through sub-directories 
     } 

     elsif ($currentFile =~ /\.tar.gz$/i || $currentFile =~ /\.tar$/i) 
     { 
         print "File: ", $currentFile, "\n\n"; 
         my $tarOutput = `tar -tvzf $currentFile`; 
         print $tarOutput, "\n"; 
     } 

     print "----------------------------------------------------------------------- \n\n"; 
    } 
} 

이 디렉토리는 결코 인쇄 경우 $ currentFile를 인쇄 할 인쇄 문 ...

는 출력 :

Searching in /home/gackerma/Logs 

------------------------------------------------------------------------ 

Current File:Dir1 

----------------------------------------------------------------------- 

Current File:file 

----------------------------------------------------------------------- 

Current File:configs.tar.gz 

File: configs.tar.gz 

tar (child): configs.tar.gz: Cannot open: No such file or directory 
tar (child): Error is not recoverable: exiting now 
tar: Child returned status 2 
tar: Error is not recoverable: exiting now 

----------------------------------------------------------------------- 

Current File:adstatlog.299 

----------------------------------------------------------------------- 

Current File:adstatlog.tgz 

----------------------------------------------------------------------- 

내가 수행 한 같은 디렉토리에 슈퍼 간단한 스크립트로 똑같은 일을하고 그것은 작동하지만 ... 아니, 아니, 여기. 나는 무엇이 잘못되었는지 이해하지 못한다 ...?

도와주세요.

답변

3

이것은 잘못된 것입니다. 나는 이중 shift 전원이 이동하는 것을 의미한다 같은데요 diff

--- yours.pl  
+++ mine.pl 

    # Open and close the directory 
    opendir(DIR, $directory) or die("ERROR: Couldn't open specified directory $!"); 
- my @files = readdir DIR; 
+ my @files = grep { $_ !~ /^\.{1,2}$/ } readdir DIR; 
    closedir DIR; 

- shift(@files); 
- shift(@files); 

    foreach my $currentFile (@files) 
    { 
+  my $fullPath = "$directory/$currentFile"; 
     print "Current File:", $currentFile, "\n\n"; 
-  if (-d $currentFile) #THIS DOESN'T WORK?!?!?!?!? 
+  if (-d $fullPath) #THIS DOESN'T WORK?!?!?!?!? 
     { 
      print "Directory: ", $currentFile, "\n\n"; 
      #push (@directories, $currentFile); 
      #print "Directories: ", @directories, "\n\n"; 
      #next; 
      # The Subroutine is calling hisself with the new parameters 
-   #readDirectory($currentFile); #recursive call through sub-directories 
+   readDirectory($fullPath); #recursive call through sub-directories 
     } 

아래를 참조하십시오 "." 및 ".."입니다. sort @files을 수행하면 문제가 해결되지만 여전히 잘못 될 수 있습니다. grep을 참조하십시오.

다음 문제는 $currentFile이 전체 경로 여야한다는 것입니다. 그렇지 않으면 현재 작업 디렉토리에 $currentFile을 찾고

+0

예 즉 교대가 무엇인지입니다. 고마워요! – user2340116

+0

@ user2340116 이가 지금은 분명합니까? 아니면이 diff 출력을 읽는 데 문제가 있습니까? – chrsblck

+0

IT WORKED !!! 아, 고마워요! 전체 디렉토리 경로가 핵심입니다. – user2340116

0

시도 :

if (-d $directory.'/'.$currentFile) 
관련 문제