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

Re: Converting absolut symlinks to relative ones...?



On Mar 6,  9:39am, Danek Duvall wrote:
}
} GNU ln has a -r option which does this.

That must be a very recent addition, there's no sign of it in coreutils
8.13 from 2011 (on my Ubuntu box).

ln-r () {
  emulate -L zsh
  local symlink
  local -a here there route common
  for symlink in $@:a
  do
    [[ -h $symlink ]] || continue
    common=()
    route=()
    if [[ -d $symlink ]]
    then here=( ${(s:/:)symlink} )
    else here=( ${(s:/:)symlink:h} )
    fi
    there=( ${(s:/:)symlink:A} )
    while (( $#here && $#there ))
    do
      if (( $#route))
      then route=( .. $route $there[1] )
      elif [[ $here[1] == $there[1] ]]
      then
        common+=( $here[1] )
      else
        if [[ -d $symlink || -z $common ]]
        then route=( $there[1] )
        else route=( .. $there[1] )
        fi
      fi
      shift here
      shift there
    done
    if (( $#common ))
    then route+=( $there )
    else continue # no common prefix, absolute link is best
    fi
    # Remove the "print" and both (qq) to actually do linking
    print ln -fs "${(qq)${(j:/:)route}:-.}" "${(qq)symlink}"
  done
}

To change the "no common prefix" behavior, change the loop to become
"while (( $#here || $#there ))" and remove references to $common.



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