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:
Nero 2018-03-16 04:02:12 +00:00
parent a375428d5d
commit 60c02a37fd
1 changed files with 35 additions and 7 deletions

View File

@ -1,11 +1,39 @@
#!/bin/sh
p="${1%/}"
target=$(readlink "$p")
p="${p#/}"
case "$target" in
/*) p="$target";;
'') ;;
*) p="${p%/*}/$target";;
esac
r=""
printf "%s\n" "$p"|sed -e 's@/[^/]*/\.\.\(/\|$\)@/@g' -e 's|/\.\(/\|$\)|/|g' -e 's|/$||g' -e '/^$/d'
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
echo "$r"