我需要在unix环境中使用Perl测试文件系统上是否存在文件/目录.我尝试使用文件test -e,但这并没有区分
我可以检查$中的内容是什么?测试后变量并搜索"权限被拒绝",但这听起来不是正确的事情.
有什么建议?
非常感谢
ike*_*ami 10
-e只是一个调用stat,和其他系统调用一样,它设置$!并%!返回false.
if (-e $qfn) {
print "ok\n";
}
elsif ($!{ENOENT}) {
print "Doesn't exist\n";
}
else {
die $!;
}
Run Code Online (Sandbox Code Playgroud)
例如,
$ mkdir foo
$ touch foo/file
$ chmod 0 foo
$ perl x.pl foo/file
Permission denied at x.pl line 10.
$ perl x.pl bar/file
Doesn't exist
$ perl x.pl x.pl
ok
Run Code Online (Sandbox Code Playgroud)