On Mon, Feb 09, 2015 at 09:00:12PM +0000, Cheng Rk wrote:
> -f FILE        True if file exists and is a regular file.
> 
> but why it returned true on a symlink to a regular file?
> 
> $ [ -f tmp/sym-link ] && echo true
> true

It's supposed to work this way.  -f resolves symlinks and tests the
target location.

If you want to determine whether something is a symlink, you need to
test that explicitly with -L or -h.

imadev:~$ ln -s /etc/passwd linktopasswd
imadev:~$ [ -f linktopasswd ] && echo true
true
imadev:~$ [ -L linktopasswd ] && echo true
true

Similarly, to test for a dangling symlink, you need to apply at least
two separate tests:

imadev:~$ ln -s nosuchthing dangling
imadev:~$ [ -f dangling ] && echo true
imadev:~$ [ -L dangling ] && echo true
true

Reply via email to