2013-10-12 3 views
0

이 순간 많은 사람들이 많은 커피와 커피를 마시는 데 도움이되는 최고의 코딩 커뮤니티에 연락하고 있습니다.파일이 더 이상 존재하지 않는 perl이 될 때까지 기다려주세요

기본적으로 진행하기 전에 파일이 더 이상 서버에 존재하지 않을 때까지 기다리는 펄 스크립트가 필요합니다. 정말로 모든 화면 세션이 종료 될 때까지 대기합니다 (/ var/run/screen은 빈 디렉토리 임).

#!/usr/bin/perl 
my $directory="/root/screens"; 

opendir(DIR, $directory) or die "couldn't open $directory: $!\n"; 
my @files = readdir DIR; 
closedir DIR; 

foreach (@files){ 

     while (-e $_) { 
     print "$directory has $_ existing\n"; 
     sleep 1; 
     } 
} 

Ex Dir.

total 8 
    drwxr-xr-x. 2 root root 4096 Oct 11 22:58 ./ 
    drwxr-x---. 17 root root 4096 Oct 11 05:33 ../ 
    -rw-r--r--. 1 root root 0 Oct 11 22:58 S-root 
    -rw-r--r--. 1 root root 0 Oct 11 22:58 S-root1 

위의 내용은 분명히 작동하지 않지만 Google에서이 작업을 수행하는 좋은 방법이 보이지 않고 있으며 긴 하루였습니다.

+0

DIR (달러)에 달러 기호가 필요 – amrfaissal

+2

아니요, DIR에 달러 기호가 필요하지 않습니다. – mob

답변

2
#!/usr/bin/perl 
my $directory="./test"; 

my $no_empty = 1; 
my @files =(); 
while($no_empty) { 
    opendir(DIR, $directory) or die "couldn't open $directory: $!\n"; 
    @files = readdir DIR; 
    #print @files; 
    if(scalar @files == 2) { 
     $no_empty = 0; 
     last; ##directory is empty 
    } else { 
     print "Below files present--"; 
     @files = grep {$_ !~ /\.{1,2}$/} @files; ## skip . (current dir) and parent dir (..) 
     print join (', ',@files); ##comma seperated file list 
     print "\n"; ##mendetory to flash the output buffer 
     sleep 1; 
    } 
} 

print "Start with processing cz dir is empty" 

샘플 실행 -에 대해 어떻게

> Below files present--def, abc 
> Below files present--def, abc 
> Below files present--def, abc 
> Below files present--def, abc 
> Start with processing cz dir is empty 
+0

완벽하게 작동합니다. 바이러스 대단히 감사합니다. – MattSizzle

0

디렉터리가 비어 있지 않은 한 실행하려면 while 루프가 필요합니다. 대상 디렉토리가 비면 프로그램이 리턴됩니다. 대상 디렉토리가 비어 있거나없는 경우

는 이제 확인하는 서브 루틴을 만들어 보자 :

sub isDirEmpty { 
     my ($dir) = @_; 
     my $file; 
     if (opendir my $DFH, $dir) { 
       while (defined ($file = readdir $DFH)) { 
         next if $file eq '.' or $file eq '..'; 
         closedir $DFH; 
         return 0; #Not Empty 
       } 
       closedir $DFH; 
       return 1; #Empty 
     } else { 
       return -1; #Doesn't exist 
     } 
} 

무한 루프 작은 테스트 :

my $targetDir = "/path/to/target/dir"; 
while (1) { 
     my $result = isDirEmpty($targetDir); 
     if ($result eq 1) { 
       print "Your directory is empty\n"; 
       last; 
     } elsif ($result eq -1) { 
       print "There has been an error! please check your lousy code!\n"; 
     } 
} 

자보세요!

+0

유효하지만 이미 큰 스크립트의 일부이므로 최대한 적은 수의 솔루션을 찾고있었습니다. 나는이 때문에 바이러스에 의해 게시 된 답변을 선택합니다. – MattSizzle

0

:

my $directory="/root/screens"; 
while(<$directory/*>) { 
    say "not empty"; 
    sleep 1; 
} 
+0

위의 내용은 디렉토리 내의 각 파일에 대해 한 번만 반복됩니다. 처음에는 glob (ed) 와일드 카드를 사용하려고했지만 그걸로는별로 행운이 없었습니다. – MattSizzle

0
#!/usr/bin/perl 
my $directory = "/root/screens"; 

while(1) { 
    my @files = <$directory/*>; 
    if($#files == -1) { 
     last; 
    } else { 
     sleep 1; 
    } 
} 

# do some stuff when the directory is empty 
0
#!/usr/bin/perl 

my $directory="/root/screens"; 

opendir(DIR, $directory) or die "couldn't open $directory: $!\n"; 
@files = readdir DIR; 
closedir DIR; 

while (@files > 2) ### If @files have more than 2 items. 
{ 
opendir(DIR, $directory) or die "couldn't open $directory: $!\n"; 
@files = readdir DIR; 
closedir DIR; 
sleep (1); 
} 

print "Directory is now empty and loop has ended"; 

주 @files 2 개 항목을 항상있을 것이라는 점을 항상있을 것 때문이다./and ../

관련 문제