On 2017-06-12, Stephane Chazelas <
stephane...@gmail.com> wrote:
> 2017-06-10 14:27:26 +0000, Kaz Kylheku:
> The first problem can be worked around by doing
>
> [ -h "${dir%/}" ]
I'm looking at this on a GNU/Linux system.
It looks like this is actually a behavior of the lstat system call.
With the trailing slash, lstat doesn't see the symlink, but resolves
through it to the directory.
POSIX seems to require it, according to Pathname Resolution.
In the IEEE Std 1003.1-2008, 2016 Edition, it says in 4.13 Pathname
Resolution:
If a symbolic link is encountered during pathname resolution, the
behavior shall depend on whether the pathname component is at the end of
the pathname and on the function being performed. If all of the
following are true, then pathname resolution is complete:
1. This is the last pathname component of the pathname.
2. The pathname has no trailing <slash>.
3. The function is required to act on the symbolic link itself,
or certain arguments direct that the function act on the
symbolic link itself.
(3) makes it clear that this whole requirement is precisely for
funtions such as lstat which act on the symlink. For the sake of
those functions, pathname resolution leaves the last component
unresolved if it is a symlink --- but only if there is no trailing
slash.