Closed p5pRT closed 20 years ago
Try this little script:
#!/usr/bin/perl
`touch ./foo`;
symlink ("./foo"\, "./foolink");
print `ls -l ./foo ./foolink`;
if (-e "./foo") {
print "1: file ./foo exists\n";
}
if (-l "./foo") {
print "1: file ./foo is a link\n";
}
if (-e "./foolink") {
print "1: file ./foolink exists\n";
}
if (-l "./foolink") {
print "1: file ./foolink is a link\n";
}
`touch tmp/foo`;
symlink ("tmp/foo"\, "tmp/foolink");
print `ls -l tmp/foo tmp/foolink`;
if (-e "tmp/foo") {
print "2: file tmp/foo exists\n";
}
if (-l "tmp/foo") {
print "2: file tmp/foo is a link\n";
}
if (-e "tmp/foolink") {
print "2: file tmp/foolink exists\n";
}
if (-l "tmp/foolink") {
print "2: file tmp/foolink is a link\n";
}
This prints:
-rw-rw-r-- 1 vlb noodle 0 Aug 12 17:16 ./foo lrwxrwxrwx 1 vlb noodle 3 Aug 12 16:53 ./foolink -> foo 1: file ./foo exists 1: file ./foolink exists 1: file ./foolink is a link -rw-rw-r-- 1 vlb noodle 0 Aug 12 17:16 tmp/foo lrwxrwxrwx 1 vlb noodle 7 Aug 12 17:04 tmp/foolink -> tmp/foo 2: file tmp/foo exists 2: file tmp/foolink is a link
I should see 2: file tmp/foolink exists
When I use if (-e "symlink") or if (-e "./symlink") and "symlink" is a symbolic link\, I get success
When I try to test a path\, such as if (-e "tmp/symlink") I get failure. However if (-l "tmp/symlink") succeeds.
If -l returns success\, then the file (a link) must also exist; but under certain circumstances\, -e fails.
\-rw\-rw\-r\-\- 1 vlb noodle 0 Aug 12 17​:16 tmp/foo lrwxrwxrwx 1 vlb noodle 7 Aug 12 17​:04 tmp/foolink \-> tmp/foo 2​: file tmp/foo exists 2​: file tmp/foolink is a link
I should see 2: file tmp/foolink exists
The bug is in you\, not in Perl. Try
cat tmp/foolink
and your OS will tell you the same thing:
plover% ls -l tmp/* -rw-r--r-- 1 mjd users 0 Aug 12 20:53 tmp/foo lrwxrwxrwx 1 mjd users 7 Aug 12 20:53 tmp/foolink -> tmp/foo plover:/tmp/vbd% cat tmp/foo plover:/tmp/vbd% cat tmp/foolink cat: tmp/foolink: No such file or directory
That is because tmp/foolink here refers to the nonexistent file tmp/tmp/foo.
If -l returns success\, then the file (a link) must also exist; but under certain circumstances\, -e fails.
-e tests the existence of the *file that is linked to*\, not the existence of the link itself. Thus -e on a dangling symlink will return false.
In \199908130018\.RAA08993@​cfcl\.com\, Vicki Brown writes: :This prints: : : -rw-rw-r-- 1 vlb noodle 0 Aug 12 17:16 ./foo : lrwxrwxrwx 1 vlb noodle 3 Aug 12 16:53 ./foolink -> foo : 1: file ./foo exists : 1: file ./foolink exists : 1: file ./foolink is a link : -rw-rw-r-- 1 vlb noodle 0 Aug 12 17:16 tmp/foo : lrwxrwxrwx 1 vlb noodle 7 Aug 12 17:04 tmp/foolink -> tmp/foo : 2: file tmp/foo exists : 2: file tmp/foolink is a link
That should be 'tmp/foolink -> foo'. The symlink works relative to foolink\, not relative to your current directory.
Note that you can check for the existence of the symlink itself with: lstat "tmp/foolink"; if (-e _) { print "tmp/foolink exists\n"; }
Hugo
Vicki Brown \vlb@​cfcl\.com writes:
If -l returns success\, then the file (a link) must also exist; but under certain circumstances\, -e fails.
This is actually useful for finding broken links.
-- Nick Ing-Simmons \nik@​tiuk\.ti\.com Via\, but not speaking for: Texas Instruments Ltd.
At 20:55 -0400 08/12/1999\, Mark-Jason Dominus wrote:
-e tests the existence of the *file that is linked to*\, not the existence of the link itself. Thus -e on a dangling symlink will return false.
Ahhhh. A subtlety not clear from the docs. -- -- |\ _\,\,\,---\,\,_ Vicki Brown \vlb@​cfcl\.com ZZZzz /\,`.-'`' -. ;-;;\,_ Journeyman Sourceror: Scripts & Philtres |\,4- ) )-\,_. \,\ ( `'-' P.O. Box 1269 San Bruno CA 94066 '---''(_/--' `-'\_) http://www.cfcl.com/~vlb http://www.macperl.com
Migrated from rt.perl.org#1211 (status was 'resolved')
Searchable as RT1211$