我需要编辑一些被破坏的软链接,因为它们是绝对的。我想让他们去一些相对的人。为此我需要计算它们指向的路径的起始目录深度。然后我需要用../替换这些计算的目录,因为我只需要起床,直到找到匹配的目录。
我已经准备好找到破碎的链接:
find -type l | while read f; do if [ ! -e "$f" ]; then ls -l "$f"; fi; done
这是有效的,所以我可以找到破碎的,但我不知道如何继续。对不起,我是写Shell-Scripts的新手。希望你能帮帮我。
非常感谢您的帮助,我发现以下解决方案有效:
depthRoot=2 # Depth of root-directory
find -type l |
while read f
do
if [ ! -e "$f" ] # broken link found (put in variable f)
then
echo "Link found: $f" # output of broken link
previous="$(readlink "$f")" # target of broken link in variable previous
echo "points to: $previous" # output of target
y=$"${f//[^\/]}" # generating string of f with all the slashs
depthFrom=${#y} # count length of y an put value in depthFrom
depthFrom=`expr $depthFrom - 1`
echo "Depth place = $depthFrom" # output of depth of place of broken link
y=$"${previous//[^\/]}" # generating string of previous with all the slashs
depthTarget=${#y} # count length of y an put value in depthTarget
depthTarget=`expr $depthTarget - $depthRoot - 1` # subtraction of depth of root-directory
echo "Depth target = $depthTarget" # output of depth of target of link
depthRoot=`expr $depthRoot + 1` # root-directory-depth +1 (1 Directory - 2 Slashs)
depth=`expr $depthFrom - $depthTarget` # calculate difference in depth
echo "difference of depth = $depth" # output of differrence of depth
while [ $depthRoot -gt 0 ] # while depthRoot > 0
do
depthRoot=`expr $depthRoot - 1` # decrement depthRoot by 1
previous=${previous#*/} # erease everything of previous until first Slash leftside
done
f=${f:2} # subtract ./ of f
tofind="$(echo "$previous"| sed -e 's/[\/&]/\\&/g')" # escape subdirectories of previous
tofind=${tofind%\\*} # erease everything on the right side until first backslash
newlink="$(echo "$f"| sed "s/$tofind//g")" # erease existing side on the left
newlink=${newlink:1}
while [ $depth -gt 0 ] # while depth > 1
do
newlinkaddition=$newlinkaddition"../" # generating ../ for relative links
newlink=${newlink#*/} # remove old directories which will be replaced by ../
depth=`expr $depth - 1` # decrement depth
done
newlink=$newlinkaddition$newlink # generating target of new link
rm $f # erease old link
ln -s "$newlink" "$" # generating new link
echo "new link generated: $newlink -> $f" # output of successfull generation of new link
fi
done
答案 0 :(得分:0)
问题是你需要知道所有断开的链接都有一个共同的根。
e.g。在我的试用版中,你的代码给了我这个输出
lrwxrwxrwx 1 user group 10 Aug 10 17:31 ./a/c -> /tmp/old/c
在这种情况下,公共根是/ tmp / old
所以必须删除并替换为新位置的相对链接,该位置应该是当前目录
oldroot=/tmp/old/ # needs / at the end!
find -type l |
while read f
do
if [ ! -e "$f" ] # dead link found
then
f="${f#./}" # remove ^./ from find result
echo "found link $f"
previous="$(readlink "$f")" # get old destination
echo "pointing to $previous"
previous="${previous#$oldroot}" # remove oldroot
newlink="$(echo "$f"|awk 'BEGIN{FS=OFS="/"}{for(i=1;i<NF;i++){printf "../"}}END{print "."}')"
newlink="$(echo "$newlink/$previous"|sed 's,/\./,/,g')"
echo "creating link $f -> $newlink"
rm $f
ln -s "$newlink" "$f"
fi
done
此解决方案使用awk和sed。也许在bash中也可以这样做,但是使用awk&amp;在这种情况下。