realpath.sh/realpath.sh

12 lines
206 B
Bash
Executable File

#!/bin/sh
p="$1"
target=$(readlink "$p")
case "$target" in
/*) p="$target";;
'') ;;
*) p="${p%/*}/$target";;
esac
printf "%s\n" "$p"|sed -e 's@/[^/]*/\.\.\(/\|$\)@/@g' -e 's|/\.\(/\|$\)|/|g' -e 's|/$||g'