bug-bash
[Top][All Lists]
Advanced

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

Re: Does [ -f FILE ] have a bug on testing a symlink ?


From: Greg Wooledge
Subject: Re: Does [ -f FILE ] have a bug on testing a symlink ?
Date: Mon, 9 Feb 2015 16:44:18 -0500
User-agent: Mutt/1.4.2.3i

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

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