#!/bin/sh # # Scott Snyder's relpathto # # Usage: relpathto PATH # # Writes to stdout the relative path from the current directory to PATH. # PATH may be either absolute or relative. # # sss Feb, 1998. # # Get the target path. targ=$1 if [ "$targ" = "" ]; then echo "Usage: $0 path" 1>&2 exit 1 fi # Make sure it's valid. if [ ! -d $targ ]; then echo "$0: Invalid path \`$targ'." 1>&2 exit 1 fi # Get the current directory. curdir=`/bin/pwd` # And the target directory. # Do it by changing to that directory and doing pwd. # This ensures that we have an absolute path. cd $targ targdir=`/bin/pwd` cd $curdir # Handle the case where curdir and targdir are the same. if [ "$curdir" = "$targdir" ] ; then echo "." exit 0 fi # Pull matching pathname components off the heads of the paths. curhead=`expr "$curdir" : '/\([^/]*\)'` targhead=`expr "$targdir" : '/\([^/]*\)'` while [ "$curhead" = "$targhead" ] ; do curdir=`expr "$curdir" : '/[^/]*\(/.*\)'` targdir=`expr "$targdir" : '/[^/]*\(/.*\)'` curhead=`expr "$curdir" : '/\([^/]*\)'` targhead=`expr "$targdir" : '/\([^/]*\)'` done # Strip off the leading slashes. curdir=`expr "$curdir" : '/\(.*\)'` targdir=`expr "$targdir" : '/\(.*\)'` # Replace every remaining component in the current path with `..'. uppath=`echo $curdir | sed -e 's@[^/][^/]*@..@g'` # Tack on the remaining target path. # Add a directory separator between them only if neither is blank. reslt=$uppath if [ "$reslt" != "" -a "$targdir" != "" ]; then reslt=$reslt/ fi # Done. echo $reslt$targdir