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"
On Wed, Jul 24, 2024 at 12:24:50AM +0700, Robert Elz wrote:
> 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).
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 ;-)
>
> 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
--
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
Home |
Main Index |
Thread Index |
Old Index