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 explanation
The reason of that failure is quite simple, but easy to forget: test(1)
, aka [
, uses 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
8 comments
wednesday 30 october 2013 à 16:50 Patrick said : #1
wednesday 30 october 2013 à 16:52 Zack Weinberg said : #2
wednesday 30 october 2013 à 16:53 Zack Weinberg said : #3
wednesday 30 october 2013 à 17:04 Tanguy said : #4
wednesday 30 october 2013 à 21:34 Guto said : #5
thursday 31 october 2013 à 00:47 russ said : #6
thursday 31 october 2013 à 09:47 Tanguy said : #7
thursday 07 november 2013 à 20:28 Victor Nițu said : #8