summaryrefslogtreecommitdiffstats
path: root/scripts/relpath.sh
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/relpath.sh')
-rwxr-xr-xscripts/relpath.sh43
1 files changed, 43 insertions, 0 deletions
diff --git a/scripts/relpath.sh b/scripts/relpath.sh
new file mode 100755
index 0000000..cee5a21
--- /dev/null
+++ b/scripts/relpath.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+# Given two pathnames, print the first relative to the second.
+#
+# Adapted from
+# https://unix.stackexchange.com/questions/573047/how-to-get-the-relative-path-between-two-directories
+
+usage() {
+ echo "usage: $0 source target" >&2
+ exit 1
+}
+
+check_abs() {
+ case "$1" in
+ /*)
+ ;;
+ *)
+ echo "? not an absolute pathname: $1"
+ usage
+ ;;
+ esac
+}
+
+[ $# = 2 ] || usage
+check_abs "$1"
+check_abs "$2"
+
+source="$1"
+target="$2"
+prefix=""
+
+source="`echo "$source" | sed -e 's%/$%%'`/"
+target="`echo "$target" | sed -e 's%/$%%'`/"
+remain=`echo "$source" | sed -e 's%^'$target'%%'`
+while [ -n "$target" ] && [ "$source" = "$remain" ]
+do
+ prefix="../$prefix"
+ target=`echo "$target" | sed -e 's%/[^/]*/$%/%'`
+ remain=`echo "$source" | sed -e 's%^'$target'%%'`
+done
+result="${prefix}${remain}"
+[ -n "$result" ] || result="."
+
+echo "$result"