A strange problem
Yesterday, I was reported a funny problem with the dokuwiki Debian package's postinst script, which contains a piece of shell script similar to that:
# Check the destination does not already exist if [ ! -e /the/destination ] then ln -s /some/file /the/destination fi
It was failing with that message:
ln: failed to
create symbolic link '/the/destination': File exists. Even
though I had just tested it did not exist!
The reason of that failure is quite simple, but easy to forget:
stat(2), which dereferences symlinks when
testing for file existence! So,
test ! -e
/the/destination does not mean “check there is no file there” but
rather “check there is either no file or only a dead symlink”.
So, the correct way to write such a piece of code is:
# Check there is not already a file or a (dead) symlink if [ ! -e /the/destination -a ! -h /the/destination ] then ln -s /some/file /the/destination fi