diff options
author | Thomas E. Dickey <dickey@invisible-island.net> | 2024-01-15 23:28:41 +0000 |
---|---|---|
committer | Thomas E. Dickey <dickey@invisible-island.net> | 2024-01-16 00:18:06 +0000 |
commit | 8ed5d3b5bf97f5a459c5bbe7f9264903cb728d88 (patch) | |
tree | 0aafabcf3dfaab7685fa0fcbaa683dafe287807e /scripts | |
parent | 3ee8300fec5d35be98863ab0101433d348c936cc (diff) | |
download | lynx-snapshots-8ed5d3b5bf97f5a459c5bbe7f9264903cb728d88.tar.gz |
snapshot of project "lynx", label v2-9-0
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/relpath.sh | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/scripts/relpath.sh b/scripts/relpath.sh new file mode 100755 index 00000000..cee5a213 --- /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" |