Question

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?

Was it helpful?

Solution

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)

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top