localpath
This commit is contained in:
parent
2bd752012c
commit
25f74b68cf
28
urchin
28
urchin
|
@ -238,22 +238,27 @@ indent() {
|
||||||
|
|
||||||
# Expand relative paths
|
# Expand relative paths
|
||||||
fullpath() {
|
fullpath() {
|
||||||
readlink -f -- "${1}"
|
if test -e "${1}"; then
|
||||||
|
readlink -f -- "${1}" | sed 's/\/*$//'
|
||||||
|
else
|
||||||
|
echo "Could not find file or directory: ${1}" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# If $1 and $2 are the same path, return with code 1
|
# If $1 is an ascestor of $2, echo the path of $2 relative $1.
|
||||||
# If $1 is a parent of $2, echo the path of $2 relative $1.
|
# If either of $1 or $2 does not exist, return with code 1
|
||||||
|
# If $1 is not an ancestor of $2, return with code 2
|
||||||
# Otherwise, return with code 2.
|
# Otherwise, return with code 2.
|
||||||
localpath() {
|
localpath() {
|
||||||
parent="$(fullpath "${1}" | sed 's/\/*$//')"
|
test -e "${1}" && test -e "${2}" || return 1 # A file is missing.
|
||||||
child="$(fullpath "${2}" | sed 's/\/*$//')"
|
|
||||||
|
parent="$(fullpath "${1}")"
|
||||||
if test "${parent}" = "${child}"; then
|
child="$(fullpath "${2}")"
|
||||||
# Same file
|
|
||||||
return 1
|
if echo "${child}" | grep "^${parent}/" > /dev/null; then
|
||||||
elif contains "${child}" "^${parent}";
|
|
||||||
# Child is really a child.
|
# Child is really a child.
|
||||||
echo "${child##"${parent}/"}"
|
echo "${child#"${parent}/"}"
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
# Child is not really a child.
|
# Child is not really a child.
|
||||||
|
@ -261,6 +266,7 @@ localpath() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contains() {
|
contains() {
|
||||||
case "$#" in
|
case "$#" in
|
||||||
1) grep "${1}" > /dev/null ;;
|
1) grep "${1}" > /dev/null ;;
|
||||||
|
|
Loading…
Reference in New Issue