I am preparing a bash script to validate symbolic links. I want to diagnose when:
- Broken by @symlink
- @symlink points to another @symlink - (fix it with the ultimate goal of a symbolic link chain)
- @symlink points to another @symlink that is broken
- The @symlinks chain is a loop
I have big problems with point 2) when diagnosing when symlink points to a symbolic link.
I tried to use readlink , but it returns the final target of the symbolic link chain instead of the symbolic link name. I tried to run it without a parameter -f, but that did not help. And then combinations with find gave me a bad result ...
Can someone help me with this problem?
Below I have inserted 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 file structure (where symlinks.sh is discussed by bash script):

source
share