免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4085 | 回复: 10
打印 上一主题 下一主题

求教,递归遍历查找符号链接文件问题。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-02-27 14:01 |只看该作者 |倒序浏览
写了一个perl script, 想找出所有的符号链接文件。比如
dir001----file1
          |---file2
          |---link1
          |---link2
          |---dir002----file3
                        |----link3
                        |----link4

这个脚本在dir001下run, 就应该找出 link1, link2, link3, link4 文件。原程序如下
      #!/usr/bin/perl -s
           use Cwd;
           sub ScanDirectory{
        my ($workdir)=shift;
        my($startdir)=&cwd;
        chdir($workdir)or die "unable to open $!\n";
        opendir(DIR,".")or die "unable to open $!\n";
        my @names=readdir(DIR);
                    closedir(DIR);
                     foreach my $name(@names){
                                 next if ($name eq ".");
                                 next if($name eq "..");
                    if(-d $name){
                     &ScanDirectory($name);

                         }
                    if(-l $name){
                      print "$name\n";
        }

                    chdir($startdir) or die"unable $!\n";
                     }
               }
              &ScanDirectory(".");

但是,运行以后发现只能找全当前目录下所有的符号链接文件,子目录下的符号链接文件只能找到第一个。我想不出错在哪里?哪位大仙帮忙看看,谢谢先!

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
2 [报告]
发表于 2007-02-27 14:06 |只看该作者
use File::Find;

论坛徽章:
0
3 [报告]
发表于 2007-02-27 14:08 |只看该作者

谢谢回复flw.

就是想弄明白为什么用 -l filename 不行。

论坛徽章:
0
4 [报告]
发表于 2007-02-27 14:13 |只看该作者
因为$name是相对路径, 而脚本的工作在当前目录下, 所以最好手动补齐全路径, 要不就chdir。

论坛徽章:
0
5 [报告]
发表于 2007-02-27 14:22 |只看该作者
但是如果我不作判断,只是打印文件名,那么所有文件名都会打印出来。这不象找不到路径啊?

论坛徽章:
0
6 [报告]
发表于 2007-02-27 14:28 |只看该作者
唉, 我罗嗦的不得要领。
当pwd在dir001下时.
-l "dir002/link3" 或-l "/path/to/dir001/dir002/link3"都成功。
但是
-l "link3"却不对。

论坛徽章:
0
7 [报告]
发表于 2007-02-27 14:34 |只看该作者
那为什么能打印出子目录底下的第一个符号链接文件?谢谢。

论坛徽章:
0
8 [报告]
发表于 2007-02-27 14:40 |只看该作者
啊, 这么回事, 我又想当然了, 不知道。

论坛徽章:
0
9 [报告]
发表于 2007-02-27 17:07 |只看该作者
循环里面那个 chdir 做什么用的?

论坛徽章:
0
10 [报告]
发表于 2007-02-27 17:25 |只看该作者
原帖由 aman13 于 2007-2-27 14:01 发表
写了一个perl script, 想找出所有的符号链接文件。比如
dir001----file1
          |---file2
          |---link1
          |---link2
          |---dir002----file3
                        |----link ...


Hi,

allways attention to absolute and relative path in opendir(...).
readdir(...) returns only relative file or dir name.
if you want to check -l under reursivie dir, should check -l too, not only -d:
i.e: sample code:

  1. #!/usr/bin/perl -s

  2. sub ScanDirectory{
  3.     my ($workdir) = shift;
  4.     opendir(DIR, $workdir)or die "unable to open $!\n";
  5.     my @names = readdir(DIR);
  6.     closedir(DIR);

  7.     foreach my $name(@names){
  8.         next if $name eq "." || $name eq "..";
  9.         my $fullname = $workdir. '/'. $name;
  10.         # don't make -l recursively!
  11.         if (-d $fullname && ! -l $fullname){
  12.             &ScanDirectory($fullname);
  13.         } else {
  14.             # display link file:
  15.             if (-l $fullname) {
  16.                 print "-l: $fullname\n";
  17.             }
  18.         }
  19.     }
  20. }

  21. &ScanDirectory($ARGV[0] ? $ARGV[0} : '.');

复制代码



-- ulmer
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP