Reputation: 38213
I have spent the last 5 hours tracking down a bug where symlinked files have been relabelled as normal files (I don't know how!). I would like to add a verification step to my build script to ensure this doesn't happen again.
In the working environment I get this:
> ls -l
... [Info] ... File1.h -> ../../a/b/File1.h
... [Info] ... File2.h -> ../../a/b/File2.h
...
In the corrupted environment I get this:
> ls -l
... [Info] ... File1.h
... [Info] ... File2.h
...
How can I make a robust script to iterate though the files in a folder and ensure that each file is a symlink?
Upvotes: 0
Views: 54
Reputation: 38213
This is the full code I went with. It also searches recursively.
for file in $(find Headers/ -name '*.h'); do
if [ ! -L "$file" ]; then
echo "Error - $file - is not a symlink. The repo has probably been corrupted"
exit 666 # evil exit code for an evil bug
fi
done
Upvotes: 1
Reputation: 289495
You can check if the file is a link or not with either -h
or -L
:
if [ -L "$your_file" ]; then
echo "this is a link"
fi
Or shorter,
[ -L "$your_file" ] && echo "this is a link"
From man test
-h FILE
FILE exists and is a symbolic link (same as -L)
-L FILE
FILE exists and is a symbolic link (same as -h)
Upvotes: 1