Reply to topic  [ 4 posts ] 
Finding the relative path between two absolute ones 
Author Message
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
Hi all,

I'm writing a bash script whereby the user passes in two paths and the script tells you the relative path from the first to the last. The only problem is that I've got no idea how to do it...

For example, the relative path between these two absolute ones
/tmp/test
~/Documents
Would be
../../Users/benlavery/Documents

It needs to be relative because I'm using the script to generate paths that will work on a number of machines and different OS's where files will be in different places, but relative to each other.

Thanks for any help,
Ben


Sun May 16, 2010 9:26 pm
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
Could you parse each path then starting at root

omit matches

and add the ..\ where needed , best way is probably a singly linked list



... There is probably a better solution though, possibly using the PATH environment variable or similar

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Sun May 16, 2010 9:35 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
Thanks for the reply Finley,
After some more Googling (thought I'd post up here as half an hour hadn't thrown up anything), I've found a solution:

Code:
#!/bin/bash

# usage: relpath from to

if [[ "$1" == "$2" ]]
then
    echo "."
    exit
fi

IFS="/"

current=($1)
absolute=($2)

abssize=${#absolute[@]}
cursize=${#current[@]}

while [[ ${absolute[$level]} == ${current[$level]} ]]
do
    (( level++ ))
    if (( level > abssize || level > cursize ))
    then
        break
    fi
done

for ((i = level; i < cursize; i++))
do
    if ((i > level))
    then
        newpath=$newpath"/"
    fi
    newpath=$newpath".."
done

for ((i = level; i < abssize; i++))
do
    if [[ -n $newpath ]]
    then
        newpath=$newpath"/"
    fi
    newpath=$newpath${absolute[i]}
done

echo "$newpath"


Sun May 16, 2010 9:45 pm
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
forquare1 wrote:
Thanks for the reply Finley,


........

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Sun May 16, 2010 11:47 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.