K
kj
How does one find the target(s) of a Unix symlink?
I guess one klugey way would be to pick through the output of
"/bin/ls -al":
sub get_target {
my ($link, $seen) = @_;
$seen ||= {};
return $link if $seen->{$link}; # circularity
return $link unless -e $link;
my $ls = (`/bin/ls -al $link`)[0]; # ick!
return $link unless $ls =~ /\s${link} ->\s+(.*?)\s*$/;
$seen->{$link} = 1;
get_target($1, $seen);
}
Is there a more civilized way to do this?
Thanks!
kj
I guess one klugey way would be to pick through the output of
"/bin/ls -al":
sub get_target {
my ($link, $seen) = @_;
$seen ||= {};
return $link if $seen->{$link}; # circularity
return $link unless -e $link;
my $ls = (`/bin/ls -al $link`)[0]; # ick!
return $link unless $ls =~ /\s${link} ->\s+(.*?)\s*$/;
$seen->{$link} = 1;
get_target($1, $seen);
}
Is there a more civilized way to do this?
Thanks!
kj