tech-userlevel archive

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

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



    Date:        Tue, 23 Jul 2024 16:16:46 +0200
    From:        <tlaronde%kergis.com@localhost>
    Message-ID:  <Zp-7Tnec2U-uH0ua%kergis.com@localhost>

  | 	-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


Home | Main Index | Thread Index | Old Index