Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: checking link files - recursive search



I'm assuming you meant for this response to go to the list, so I'm
replying there.

On 7/29/07, pol <linux_milano@xxxxxxxx> wrote:
> Matt Wozniski wrote:
>
> > On 7/28/07, pol <linux_milano@xxxxxxxx> wrote:
> > so, you should be able to just use
> > file=$(readlink -f "$file")
> > assuming you'd like to resolve links recursively, so that a link to a
> > link to a file resolves to the file and not the middle link.
>
> You are right, my script does not perform recursive search, as i would.
> Using readlink would be a solution.
> I am wondering whether Zsh can meet that request internally, without
> resorting to external programs

function resolve_recursive {
  (
    while :; do
      cd $1:h
      local file=$1:t
      file=$(stat -L +link $file)
      if [ -z "$file" ]; then
        echo $PWD/$1:t
        break
      fi
      argv[1]=$file
    done
  )
}

The cd's are the simplest way I could think of to handle symlinks
using relative paths to different directories.  The enclosing '(' and
')' make the entire function execute in a subshell, so that your
actual PWD isn't modified.  When it resolves the file completely, it
prints out the absolute path to it and exits.

~Matt



Messages sorted by: Reverse Date, Date, Thread, Author