Here is a little script to retarget old-wrong symlinks that I want to make interactive.
#!/bin/bash
# retarget (broken) symbolink links interactively
echo -n "Enter the source directory where symlinks path should be retargeted > "
read response1
if [ -n "$response1" ]; then
symlinksdirectory=$response1
fi
if [ -d $symlinksdirectory ]; then
echo -n "Okay, source directory exists. Now enter 1) the symlinks OLD-WRONG target directory > "
read response2
if [ -n "$response2" ]; then
oldtargetdir=$response2
fi
echo -n "$oldtargetdir - And 2) enter the symlinks CORRECT target directory > "
read response3
if [ -n "$response3" ]; then
goodtargetdir=$response3
fi
echo -n "Now parsing symlinks in $symlinksdirectory to retarget them from $oldtargetdir to $goodtargetdir > "
find $symlinksdirectory -type l | while read nullsymlink ;
do wrongpath=$(readlink "$nullsymlink") ;
right=$(echo "$wrongpath" | sed s'|$oldtargetdir|$goodtargetdir|') ;
ln -fs "$right" "$nullsymlink" ; done
fi
It does not replace the symlinks' path. My bad syntax as it works fine when replacing variables with real paths for sed
(end of the script):
right=$(echo "$wrongpath" | sed s'|/mnt/docs/dir2|/mnt/docs/dir1/dir2|') ;
How should I insert variables properly?