Für einen Tinderbox patch an dem ich arbeite brauchte ich ein Skript, das mir für irgend einen Pfad einen absoluten Pfad zurückgibt. Knackpunkt ist hier, das der Pfad nicht zwangsweise schon existieren muss. Das Skript muss den Pfad dann soweit wie möglich verfolgen und den Rest der noch nicht existiert beibehalten. Einer der Knackpunkte waren da Symlinks auf nicht existierende Pfade.
Mein Lösungsansatz (erst mal als separates Skript zum Testen) sieht so aus:
Mein Lösungsansatz (erst mal als separates Skript zum Testen) sieht so aus:
Code:
#!/bin/sh -f
follow() {
local path tail link
# Keep the portion to be resolved
path=$1
# The tail of the path that has been confirmed unresolvable
tail=
while [ -n "${path}" -a ! -d "${path}" ]; do
if [ -L "${path}" ]; then
# Apparently this is a symlink, where the target
# doesn't exist, resolve it
link="$(readlink "${path}")"
case $link in
/*)
path=${link}
;;
*)
path=${path%/}
path=${path%$(basename ${path})}${link}
;;
esac
continue
fi
tail=${path##*/}${tail}
path=${path%$(basename ${path})}
tail=${path:+/}${tail}
path=${path%/}
done
# Preserve / at end that will be stripped by realpath
if [ -z "${tail}" -a "${path}" != "/" ]; then
tail=${path%/}
tail=${path#${tail}}
fi
echo "${path:+$(realpath ${path})}${tail}"
}
for arg in "$@"; do
follow "$arg"
done
Zuletzt bearbeitet: