bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to test if a link exists


From: Mark Young
Subject: How to test if a link exists
Date: Fri, 21 Jun 2013 06:43:58 -0700 (PDT)
User-agent: G2/1.0

Hi,

I stumbled into discovering that the -e test for a file does not report the 
file as existing if the file is a dead symbolic link. This seems wrong to me.

Here's some test code:-
(WARNING it includes rm -f a b)

#!/bin/bash

bash --version
echo ""

rm -f a b
ln -s b a
[ -a a ] && echo "1. (test -a) File a exists, it's a dead link"
[ -e a ] && echo "1. (test -e) File a exists, it's a dead link"
[ -f a ] && echo "1. (test -f) File a exists, it's a dead link"

touch b
[ -a a ] && echo "2. (test -a) File a exists, it points to b"
[ -e a ] && echo "2. (test -e) File a exists, it points to b"
[ -f a ] && echo "2. (test -f) File a exists, it points to b"


When run on my CentOS v5.9 system I get the following 

$ ./test
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

2. (test -a) File a exists, it points to b
2. (test -e) File a exists, it points to b
2. (test -f) File a exists, it points to b


When run on Cygwin I also get basically the same

$ ./test
GNU bash, version 4.1.10(4)-release (i686-pc-cygwin)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

2. (test -a) File a exists, it points to b
2. (test -e) File a exists, it points to b
2. (test -f) File a exists, it points to b


My feeling is that this is wrong and that I should be told that a exists even 
though b doesn't. File 'a' does exist it is a dead symbolic link. So it 
prevents me for instance creating a symbolic link:-

E.g.

$ ln -s c a
$ ls -l a b c 
ls: b: No such file or directory
ls: c: No such file or directory
lrwxrwxrwx 1 marky tools 1 Jun 21 14:41 a -> b


Is this an error in bash?
What test should I use to decide if a file exists (including dead symbolic 
links)?

Cheers,
Mark


reply via email to

[Prev in Thread] Current Thread [Next in Thread]