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 14:09:06 +1000

On Tue, Aug 9, 2011 at 1:36 PM, Bob Proulx <bob@proulx.com> wrote:
> Jon Seymour wrote:
>> readlink -f will fully resolve links in the path itself (rather than
>> link at the end of the path), which was the behaviour I needed.
>
> Ah, yes, well, as you could tell that was just a partial solution
> anyway.
>
>> It seems cd -P does most of what I need for directories and so
>> handling things other than directories is a small tweak on that.
>
> You might try cd'ing there and then using pwd -P to get the canonical
> directory name.  I am thinking something like this:
>
>  #!/bin/sh
>  p="$1"
>  dir=$(dirname "$p")
>  base=$(basename "$p")
>  physdir=$(cd "$dir"; pwd -P)
>  realpath=$(cd "$dir"; ls -l "$base" | awk '{print$NF}')
>  echo "$physdir/$realpath" | sed 's|//*|/|g'
>  exit 0
>
> Again, another very quick and partial solution.  But perhaps something
> good enough just the same.

> realpath=$(cd "$dir"; ls -l "$base" | awk '{print$NF}')

I always use sed for this purpose, so:

   $(cd "$dir"; ls -l "$base" | sed "s/.*->//")

But, with pathological linking structures, this isn't quite enough -
particularly if the target of the link itself contains paths, some of
which may contain links :-)

jon.



reply via email to

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