为什么Perl文件测试运算符"-l"在以下条件下无法检测到符号链接?
系统信息
john@testbed-LT:/temp2/test$ uname -a
Linux Apophis-LT 4.13.0-37-generic #42-Ubuntu SMP Wed Mar 7 14:13:23 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
john@testbed-LT:/temp2/test$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 17.10
Release: 17.10
Codename: artful
Run Code Online (Sandbox Code Playgroud)
Perl信息
john@testbed-LT:/temp2/test$ perl -v
This is perl 5, version 26, subversion 0 (v5.26.0) built for x86_64-linux-gnu-thread-multi (with 56 registered patches, see perl -V for more detail)
Run Code Online (Sandbox Code Playgroud)
测试资源
john@testbed-LT:/temp2/test$ touch regular_file
john@testbed-LT:/temp2/test$ mkdir dir
john@testbed-LT:/temp2/test$ ln -s regular_file symlink
john@testbed-LT:/temp2/test$ ls -al
total 12
drwxrwxr-x 3 john john 4096 May 6 02:29 .
drwxrwxrwx 6 john john 4096 May 6 02:29 ..
drwxrwxr-x 2 john john 4096 May 6 02:29 dir
-rw-rw-r-- 1 john john 0 May 6 02:29 regular_file
lrwxrwxrwx 1 john john 12 May 6 02:29 symlink -> regular_file
Run Code Online (Sandbox Code Playgroud)
包含失败"-l"运算符的脚本
john@testbed-LT:/temp2/test$ cat ~/.scripts/test.pl
#!/usr/bin/perl
use strict;
use warnings;
use Cwd 'abs_path';
my $targetDir = "/temp2/test";
opendir(DIR, $targetDir) || die "Can't open $targetDir: $!";
while (readdir DIR) {
my $file = "$_";
if($file =~ m/^\.{1,2}/) {
next;
}
$file = abs_path($file);
if(-l "$file") {
print "Link: $file\n";
}
elsif(-d "$file") {
print "Dir: $file\n";
}
elsif(-f "$file") {
print "File: $file\n";
}
else {
print "\n\n *** Unhandled file type for file [$file]!\n\n";
exit 1;
}
}
closedir(DIR);
Run Code Online (Sandbox Code Playgroud)
脚本输出
john@testbed-LT:/temp2/test$ perl ~/.scripts/test.pl
File: /temp2/test/regular_file
Dir: /temp2/test/dir
File: /temp2/test/regular_file
Run Code Online (Sandbox Code Playgroud)
我正试图解决的问题
请注意上面的输出中没有列出符号链接(名为"symlink"),而文件"regular_file"列出两次(我希望列出"symlink" - 实际链接而不是它指向的文件).
当我更改... if(-l "$file") ...到... if(lstat "$file") ...脚本,再次"符号链接"没有列出而"regular_file"列出两次,但他们正在从旨在捕获符号链接,块内上市即:
john@testbed-LT:/temp2/test$ perl ~/.scripts/test.pl
Link: /temp2/test/regular_file
Link: /temp2/test/dir
Link: /temp2/test/regular_file
Run Code Online (Sandbox Code Playgroud)
目标
我想要实现的输出(在下面伪造 - 实际上不是由脚本生成,而是手工生成)是:
john@testbed-LT:/temp2/test$ perl ~/.scripts/test.pl
File: /temp2/test/regular_file
Dir: /temp2/test/dir
Link: /temp2/test/symlink
Run Code Online (Sandbox Code Playgroud)
......但不一定按顺序排列(我不关心列表的顺序).
为什么上面显示的脚本没有达到上述目标(为什么"-l"运算符不起作用)?
绝对路径
Run Code Online (Sandbox Code Playgroud)my $abs_path = abs_path($file);使用与getcwd()相同的算法.符号链接和相对路径组件("."和"..")被解析为返回规范路径名,就像realpath(3)一样.返回错误时
undef,$!设置为指示错误.
(强调我的.)
如果要查看符号链接,请不要使用abs_path.
你想要做的是
$file = "$targetDir/$file";
Run Code Online (Sandbox Code Playgroud)
即前缀您读取的目录的名称$file.
补充说明:
opendir(DIR, $targetDir) || die "Can't open $targetDir: $!";
while (readdir DIR) {
my $file = "$_";
Run Code Online (Sandbox Code Playgroud)
应该
opendir(my $dh, $targetDir) || die "Can't open $targetDir: $!";
while (my $file = readdir $dh) {
Run Code Online (Sandbox Code Playgroud)
"$_".$_要$file在下一步中将字符串复制到第一个时分配?