[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: equivalent of Linux readlink -f in pure bash?

From: Bob Proulx
Subject: Re: equivalent of Linux readlink -f in pure bash?
Date: Mon, 8 Aug 2011 22:51:26 -0600
User-agent: Mutt/1.5.21 (2010-09-15)

Jon Seymour wrote:
> readlink_f()
> {
>         local path="$1"
>         test -z "$path" && echo "usage: readlink_f path" 1>&2 && exit 1;

An extra ';' there that doesn't hurt but isn't needed.

>         local dir
>         if test -L "$path"
>         then
>                 local link=$(ls -l "$path" | sed "s/.*-> //")

I would be inclined to also look for a space before the " -> " too.
Because it just is slightly more paranoid.

                local link=$(ls -l "$path" | sed "s/.* -> //")

>                 if test "$link" = "${link#/}"
>                 then
>                         # relative link
>                         dir="$(dirname "$path")"

As an aside you don't need to quote assignments.  They exist inside
the shell and no word splitting will occur.  It is okay to assign
without quotes here and I think it reads slightly better without.

                        dir=$(dirname "$path")

>                         readlink_f "${dir%/}/$link"
>                 else
>                         # absolute link
>                         readlink_f "$link"
>                 fi
>         elif test -d "$path"
>         then
>                 (cd "$path"; pwd -P) # normalize it
>         else
>                 dir="$(cd $(dirname "$path"); pwd -P)"
>                 base="$(basename "$path")"

Same comment here about over-quoting.  If nothing else it means that
syntax highlighting is different.

                dir=$(cd $(dirname "$path"); pwd -P)
                base=$(basename "$path")

>                 echo "${dir%/}/${base}"
>         fi
> }

And of course those are just suggestions and nothing more.  Feel free
to ignore.

Note that there is a recent movement to change that dash greater-than
combination into a true unicode arrow graphic emited by 'ls'.  I think
things paused when there were several different bike shed suggestions
about which unicode arrow symbol people wanted there.  I haven't seen
any actual movement for a while and I think that is a good thing.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]