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
42
realpath.sh
42
realpath.sh
@ -1,11 +1,39 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
p="${1%/}"
|
p="${1%/}"
|
||||||
target=$(readlink "$p")
|
p="${p#/}"
|
||||||
|
|
||||||
case "$target" in
|
r=""
|
||||||
/*) p="$target";;
|
|
||||||
'') ;;
|
|
||||||
*) p="${p%/*}/$target";;
|
|
||||||
esac
|
|
||||||
|
|
||||||
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"
|
||||||
|
Loading…
Reference in New Issue
Block a user