I am preparing a bash script to validate correctness of symlinks. I want to diagnose when:
- @symlink is broken
- @symlink points to another @symlink -- (fix it with final target of symlinks chain)
- @symlink points to another @symlink, which is broken
- @symlinks chain is a cycle
I have big problems with point 2) in diagnosing when symlink points to symlink.
I was trying to use readlink, but it returns final target of symlinks chain instead of pointed symlink name. I tried to run it without -f
parameter, but it wasn't help. And then combinations with find gave me poor result...
Can anyone help me with this issue?
Below I pasted my code in the current version.
failed=0
for file in path/*
do
if [[ -L "$file" ]]
then
if [[ ! -a "$file" ]]
then
echo "Symlink '$file' is broken -- target object doesn't exists."
failed=1
elif [[ -L "$(readlink -f $file)" ]]
then
echo "Symlink '$file' points to another symlink: '$(readlink $file)'"
failed=1
fi
fi
done
exit $failed
UPDATE
Testing files structure (where symlinks.sh is discussed bash script):