fperal:
Correct me if I’m wrong, but for instance
/home/fperal/.local/share/akonadi/socket-tutatis2 which points to tmp/akonadi-fperal.5sdlq4 and should point to /tmp/akonadi-fperal.5sdlq4 will not be corrected with that because you search in /usr and test if the link is pointing to usr/*
I have tried this
tutatis:/imagenes/scripts # cat findlinks.sh
#!/bin/bash
find $1 -xtype l -exec ./repairbrokenlink.sh {} \;
tutatis:/imagenes/scripts # cat repairbrokenlink.sh
#!/bin/bash
#echo File
#echo $1
RELATIVEPATH=$(readlink $1)
#echo symlink relative path
#echo $RELATIVEPATH
ABSOLUTEPATH="/${RELATIVEPATH}"
#echo symlink absolute path
#echo $ABSOLUTEPATH
if ${RELATIVEPATH:0:1} != "/" ] && -e "$ABSOLUTEPATH" ] ;
then
echo repairing symlink
ln -sf $ABSOLUTEPATH $1
echo "File: $1 Broken link: $RELATIVEPATH Replaced with $ABSOLUTEPATH "
echo "File: $1 Broken link: $RELATIVEPATH Replaced with $ABSOLUTEPATH " >> repairbrokenlinks.log
fi
And it works mostly, but for some links it returns an error
./repairbrokenlink.sh: line 15: : !=: unary operator expected
I think something is not well quoted.
Try that:
erlangen:~ # cat /root/bin/findlinks.sh
#!/bin/bash
if -z $1 ]]; then
echo Usage: findlinks.sh folderpath
exit
fi
find $1 -type l|while read n ; do
t=$(readlink "$n")
r=$(realpath "$n")
if ! -e "$t" ]] && ! -e "$r" ]] ; then
echo link name "$n": target "$t" does not exist.
if "$t" == ../* ]] ; then
echo ln -sf "$r" "$n"
else
echo ln -sf /"$t" "$n"
fi
fi
done
erlangen:~ #
Some test:
erlangen:~ # ln -sf usr/bin/ls /home/link
erlangen:~ # findlinks.sh /home
realpath: /home/link: No such file or directory
link name /home/link: target usr/bin/ls does not exist.
ln -sf /usr/bin/ls /home/link
erlangen:~ #
fperal
September 15, 2022, 8:52pm
22
arvidjaar:
It is very unlikely if you tried it with your find command. Show the actual invocation with error.
Yes, you need to quote such name on command line, but it does not apply to find invocation. And your script of course lacks quoting completely, so readlink treats each space separated part as separate filename and returns empty string. This is common mistake in shell programming.
In that script I did i should have quoted the variables like this, right?
[FONT=monospace]#!/bin/bash
**if** **** -L "$1" ];
**then**
**echo** File
**echo** "$1"
RELATIVEPATH=$(readlink "$1")
**echo** symlink relative path
**echo** "$RELATIVEPATH"
ABSOLUTEPATH="/${RELATIVEPATH}"
**echo** symlink absolute path
**echo** "$ABSOLUTEPATH"
**if** **** "${RELATIVEPATH:0:1}" **!**= "/" ] && **** -e "$ABSOLUTEPATH" ] ;
**then**
**echo** repairing symlink
ln -sf $ABSOLUTEPATH $1
**echo** "File: $1 Broken link: $RELATIVEPATH Replaced with $ABSOLUTEPATH "
**echo** "File: $1 Broken link: $RELATIVEPATH Replaced with $ABSOLUTEPATH " >> repairbrokenlinks.log
**fi**
**else**
**echo** "Not a symlink: $1"
**fi**
[/FONT]