Discussion:
test.1 -> "if file exists" -> "if pathname resolves to an existing directory entry"
(too old to reply)
t***@kergis.com
2024-07-23 14:16:46 UTC
Permalink
FWIW, the current test.1 man page could, IMHO, be improved by
mimicking POSIX wording i.e. by replacing "if file exists" by the
corresponding variant of "if pahtname resolves to an existing
directory entry...".

Context: I wondered how to test that a file exists in all cases, that
is, if it is a symlink, testing that it is pointing to an existing file,
and the current description of "-e":

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

This explaining that.
--
Thierry Laronde <tlaronde +AT+ kergis +dot+ com>
http://www.kergis.com/
http://kertex.kergis.com/
Key fingerprint = 0FF7 E906 FBAF FE95 FD89 250D 52B1 AE95 6006 F40C

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Robert Elz
2024-07-23 17:24:50 UTC
Permalink
Date: Tue, 23 Jul 2024 16:16:46 +0200
From: <***@kergis.com>
Message-ID: <Zp-7Tnec2U-***@kergis.com>

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

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Robert Elz
2024-07-23 21:51:58 UTC
Permalink
Date: Tue, 23 Jul 2024 13:46:48 -0400 (EDT)
From: Mouse <***@Rodents-Montreal.ORG>
Message-ID: <***@Stone.Rodents-Montreal.ORG>

| Yes, but it requires that you're running as a user which can write to
| the target filesystem, and, if the target doesn't exist, it will create
| it.

All true.

| For some purposes, stat -L might help, though in a small test I
| just did that printed data for the link when applied to an existing
| link whose target is nonexistent.

Which would not be a problem for the OP, whose intent seemed to be
to be able to test for a name existing, regardless of what type it is.

stat -qL -f '' file

looks like it would do admirably for that purpose.

kre


--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Mouse
2024-07-23 17:46:48 UTC
Permalink
Post by Robert Elz
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.
Yes, but it requires that you're running as a user which can write to
the target filesystem, and, if the target doesn't exist, it will create
it. For some purposes, stat -L might help, though in a small test I
just did that printed data for the link when applied to an existing
link whose target is nonexistent.

/~\ The ASCII Mouse
\ / Ribbon Campaign
X Against HTML ***@rodents-montreal.org
/ \ Email! 7D C8 61 52 5D E7 2D 39 4E F1 31 3E E8 B3 27 4B

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
t***@kergis.com
2024-07-24 12:45:23 UTC
Permalink
Post by Robert Elz
Date: Tue, 23 Jul 2024 16:16:46 +0200
| -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
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).
I will argue that the sentence is not in the correct place. It should
be at the head before describing the options.

For me, requiring to read the whole man page before attempting to
parse it (because one might incorrectly parse it because something is
defined, not conspicuously, _after_ being used) is suboptimal. Not to
mention that the form:

-e file True if file exists (regardless of type).
-h file True if file exists and is a symbolic link.

yields automatically the identification of the first "file" to the
second one i.e.: "file" is whatever name you gave (subject to standard
fully qualification relative to directories) without any
indirection.

I usually invoke sed(1) with the quit action (sed -n '5{p;q;}') once I
get what I was after, typically extracting a header or an excerpt from
a header, not wasting cycles to read a whole (perhaps huge) file I'm
not interested in except after this. For a manpage, I'm doing
the same ;-)
Post by Robert Elz
$ 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.
$ 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
--
Thierry Laronde <tlaronde +AT+ kergis +dot+ com>
http://www.kergis.com/
http://kertex.kergis.com/
http://nunc-et-hic.fr/
Key fingerprint = 0FF7 E906 FBAF FE95 FD89 250D 52B1 AE95 6006 F40C

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Loading...