linux - Perl 目录查找器不起作用?

标签 linux perl unix if-statement directory

我有一个脚本来查找目录和 .tar.gz 文件。无论出于何种原因(这就是我希望你能帮助我的原因)我的

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 是一个目录,则打印语句将打印 $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

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

我在同一目录中使用一个 super 简单的脚本做了完全相同的事情,并且它有效......但是不,不是在这里。我不明白怎么了...?

请帮忙?

最佳答案

这看起来是错误的。请参阅下面的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
         }

我猜测双 shift 的意思是移掉“.”。和“..”,这是不正确的。如果您对 @files 进行排序,这可能会起作用,但仍然是一个糟糕的方法。请参阅grep .

下一个问题是 $currentFile 需要是此处的完整路径。否则,它将在当前工作目录中查找 $currentFile

关于linux - Perl 目录查找器不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16470436/

相关文章:

Linux:如何显示同一日期内的文件

linux - 我将查询保存在 shell 脚本中的临时文件位置

perl - 从 Perl 脚本异步执行 Bash 命令

windows - 从 Windows 创建一个 unix 符号链接(symbolic link)(最好通过 python)

linux - 如何将所有文件从一个目录移动(并覆盖)到另一个目录?

java - Maven,使用antrun复制文件时出错

python - 在 Linux 下模板文件更改时重新加载 Flask 应用程序

perl hash hash 中奇数个元素

windows - "system"的 Perl Windows 替代品(打开多个进程)

c - C 中的 UDP 服务器/客户端 - 发送到错误 : Address family not supported by protocol