I would like a ver开发者_运维技巧sion of "readlink -f" that provides a trace of every individual symlink resolution it performs. Something like:
$ linktrace /usr/lib64/sendmail
/usr/lib64 -> lib
/usr/lib/sendmail -> ../sbin/sendmail
/usr/sbin/sendmail
$
I know I have used this utility in the past, on linux, and also remember at the time thinking "the name of this tool is completely unintuitive and I will forget it". Well, that day has arrived.
NOBODY WINS. The correct answer is "namei".
This Serverfault answer (in Bash) may be helpful (although it does not claim to be handling all edge cases).
Code golf anyone?
#!/usr/bin/perl
use File::Spec;
my $g;
my $f = shift;
while (1) {
print $f;
$g = readlink($f);
last unless defined $g;
printf " -> %s\n", $g;
$f =~ s,/[^/]*$,,;
$f = File::Spec->rel2abs($g, $f);
}
print "\n";
Ok, how about this:
#!/usr/bin/perl
use File::Spec;
sub r {
my ($p, $s) = @_;
my $l = readlink $p;
if ($l) {
printf "%s -> %s\n", $p, $l;
$p =~ s,/[^/]*$,,;
r("",File::Spec->rel2abs($l,$p) . $s)
} else {
$s =~ s!^(/?[^/]+)(.*)! r($p.$1, $2) !e;
}
}
r("",shift);
The output is not quite as described but it's understandable. And dig that craaazy recursive executable regexp substitution!
精彩评论