Rewrite using an while loop
This is now able to: - deal with cascaded symlinks like a->b, b->c - deal with b being a symlink in a/b/c
This commit is contained in:
parent
a375428d5d
commit
60c02a37fd
40
realpath.sh
40
realpath.sh
@ -1,11 +1,39 @@
|
||||
#!/bin/sh
|
||||
p="${1%/}"
|
||||
target=$(readlink "$p")
|
||||
p="${p#/}"
|
||||
|
||||
case "$target" in
|
||||
/*) p="$target";;
|
||||
'') ;;
|
||||
*) p="${p%/*}/$target";;
|
||||
r=""
|
||||
|
||||
while [ -n "$p" ]; do
|
||||
case "$p" in
|
||||
*/*)
|
||||
seg="${p%%/*}"
|
||||
p="${p#*/}"
|
||||
;;
|
||||
*)
|
||||
seg="$p"
|
||||
p=""
|
||||
;;
|
||||
esac
|
||||
case "$seg" in
|
||||
.)
|
||||
r="${r%/*}"
|
||||
continue
|
||||
;;
|
||||
..)
|
||||
r="${r%/*/*}"
|
||||
continue
|
||||
;;
|
||||
esac
|
||||
r="$r/$seg"
|
||||
target="$(readlink "$r")"
|
||||
case "$target" in
|
||||
/*) p="${target}"
|
||||
r="";;
|
||||
'') ;;
|
||||
*) p="${p%/*}/${target}";;
|
||||
esac
|
||||
p="${p#/}"
|
||||
done
|
||||
|
||||
printf "%s\n" "$p"|sed -e 's@/[^/]*/\.\.\(/\|$\)@/@g' -e 's|/\.\(/\|$\)|/|g' -e 's|/$||g' -e '/^$/d'
|
||||
echo "$r"
|
||||
|
Loading…
Reference in New Issue
Block a user