Jeremy Huddleston <[EMAIL PROTECTED]> writes:
> Ok, I'm sure someone has some code for this lying around, so I really
> don't want to reinvent the wheel when I've got so much else to do...
> so...
>
> Does anyone know a way to find the relative path between two absolute
> paths... for instance, I want something which behaves like:
>
> # find_relative_path /usr/include/asm-sparc /usr/include/asm
> ../asm-sparc
>
> If it's a bash function, that would be great, but relying on things in
> 'emerge system' would be alright.
>
> Thanks.
There is a nice function for that in the perlmodule File::Spec
perl -MFile::Spec -e 'print
File::Spec->abs2rel("/usr/include/asm-sparc","/usr/include/asm"),"\n"'
The utility find_relative_path is then very easy to write.
----------------------------------------------------------------------------
#! /usr/bin/perl
use strict;
use warnings;
use Pod::Usage;
use File::Spec;
pod2usage unless ( @ARGV == 2 );
my($to,$from) = @ARGV;
print File::Spec->abs2rel($to,$from),"\n";
#
# Plain Old Documentation
#
=head1 NAME
find_relative_path - find the relative path between two dirs.
=head1 SYNOPSIS
B<find_relative_path> I<to> I<from>
=head1 DESCRIPTION
B<find_relative_path> finds the shortest relative path between two directories.
=cut
--
[email protected] mailing list