bug-bash
[Top][All Lists]
Advanced

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

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


From: Jon Seymour
Subject: Re: equivalent of Linux readlink -f in pure bash?
Date: Tue, 9 Aug 2011 15:12:34 +1000

On Tue, Aug 9, 2011 at 2:51 PM, Bob Proulx <bob@proulx.com> wrote:
> 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.

Tips appreciated, thanks.

jon.

reply via email to

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