in reply to verifying symbolic links

Your code will fail on relative symbolic links, for example:
/home/me/links/foo -> ../bar
Resolving symlinks is trickier than it seems. Let the operating system do it for you instead of trying to do it yourself with readlink.
print "Bad link: $link -> $rc\n" if -l $link && ! -e $link;

Replies are listed 'Best First'.
Re: Re: verifying symbolic links
by eLore (Hermit) on Jul 25, 2001 at 17:09 UTC
    UPDATE

    Somehow my books forgot to list the "-l" file test operator. Thanks for the tip. Based on your tip, here's my new code. I need to do some additional formatting, but I think it will work quite nicely.

    #!/opt/perl5/bin/perl $THISDIR="links/"; opendir THISDIR, "$THISDIR" or die "AACK: $!"; @allfiles = grep !/^\./, readdir THISDIR; closedir THISDIR; foreach $link (@allfiles){ $rc=readlink "$THISDIR$link"; if ((-l "$THISDIR$link") && ! -e $rc){ print "Target for link \[$link\]:\t does not exist\n"; }elsif (-l "$THISDIR$link" && -e $rc){ print "Target for link \[$link\]:\t $rc\n"; }else{ print "File \[$link\] found in links directory\n"; } }
    /UPDATE