2011-08-9, 11:44(+10), Jon Seymour:
> Has anyone ever come across an equivalent to Linux's readlink -f that
> is implemented purely in bash?
>
> (I need readlink's function on AIX where it doesn't seem to be available).
[...]

What about:

readlink_f() (
  link=$1 max_iterations=40
  while [ "$max_iterations" -gt 0 ]; do
    max_iterations=$(($max_iterations - 1))
    dir=$(dirname -- "$link") || exit
    base=$(basename -- "$link") || exit
    dir=$(cd -P -- "$dir" && pwd -P) || exit
    link=${dir%/}/$base
    if [ ! -L "$link" ]; then
      printf '%s\n' "$link"
      exit
    fi
    link=$(ls -ld -- "$link") || exit
    link=${link#* -> }
  done
  printf >&2 'Loop detected\n'
  exit 1
)

-- 
Stephane

Reply via email to