Re: test.1 -> "if file exists" -> "if pathname resolves to an existing directory entry"

Robert Elz <[email protected]>
Newsgroups gmane.os.netbsd.devel.userlevel
Message-ID <[email protected]>
    Date:        Tue, 23 Jul 2024 16:16:46 +0200
    From:        <[email protected]>
    Message-ID:  <[email protected]>

  | 	-e file       True if file exists (regardless of type).
  |
  | let me wondering: what "file" is supposed to exist? The symlink by
  | itself? or what it points to?

Note the following wording in test(1) just before the EXIT STATUS
section heading:

     Note that all file tests with the exception of -h and -L follow symbolic
     links and thus evaluate the test for the file pointed at.

Which is another way of saying that they use stat(2) rather than lstat(2)
except for -h (and the obsolete -L).

You can verify this by doing:

   $ ln -s /foo/bar /tmp/SL
   $ test -e /tmp/SL && echo SL exists
   $

Needless to say the assumption here is that /foo/bar does not exist.

To achieve what you want:

   $ test -e /tmp/SL || test -h /tmp/SL && echo SL exists or is a symlink
   SL exists or is a symlink
   $

If you need a single (unraceable) test, that can often be achieved by
attempting to make a link to the target filename, as link(2) and hence
ln(1) without -f will fail if the target name exists.

kre
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.