test -h 和 test -L 之间的区别

发布于 2024-08-17 02:04:20 字数 361 浏览 3 评论 0原文

ksh shell 中的 test -L filenametest -h filename 有什么区别。从手册页来看,两者都用于识别符号链接,但我想知道确切的区别。

这是手册页中的描述。

 -h file                 True if file exists and  is  a  sym-
                         bolic link.
 -L file                 True if file exists and  is  a  sym-
                         bolic link.

What is the difference between test -L filename and test -h filename in ksh shell. From the man page, both were used to identify a symbolic link, but I want to know the exact difference.

Here is the description from the man page.

 -h file                 True if file exists and  is  a  sym-
                         bolic link.
 -L file                 True if file exists and  is  a  sym-
                         bolic link.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不忘初心 2024-08-24 02:04:21

作者对未来的希望之外,这两个选项的处理方式完全相同:

        case 'L':
        case 'h': /* undocumented, and hopefully will disappear */
            if(*arg==0 || arg[strlen(arg)-1]=='/' || lstat(arg,&statb)<0)
                    return(0);
            return(S_ISLNK(statb.st_mode));

文件 bltins/test.c 中的 ksh93 源代码表明,除了 我的结论是它们的行为完全相同,但 -h 是一个遗留选项,可能有一天会消失:-)

The source code for ksh93, in file bltins/test.c, shows that these two options are treated exactly the same, except for the author's hopes for the future:

        case 'L':
        case 'h': /* undocumented, and hopefully will disappear */
            if(*arg==0 || arg[strlen(arg)-1]=='/' || lstat(arg,&statb)<0)
                    return(0);
            return(S_ISLNK(statb.st_mode));

From this I conclude that they behave exactly the same, but that -h is a legacy option and may one day disappear :-)

养猫人 2024-08-24 02:04:21

看来它们的存在都是出于遗留原因,以便在不同版本的 Unix 之间兼容。您应该能够使用其中任何一个,因为它们执行完全相同的操作,但请注意,如果您运行的系统不符合最新标准,则可能会缺少其中一个。

这两种形式都存在于单一 Unix 规范版本 3/POSIX 2004 中,没有任何警告:

-h 路径名
如果路径名解析为存在且是符号链接的文件,则为真。如果无法解析路径名,则为 False,或者
如果 pathname 解析为存在但不是符号链接的文件。如果路径名的最后一个组成部分是
符号链接,不遵循该符号链接。
-L  路径名
如果路径名解析为存在且是符号链接的文件,则为真。如果无法解析路径名,则为 False,或者
如果 pathname 解析为存在但不是符号链接的文件。如果路径名的最后一个组成部分是
符号链接,不遵循该符号链接。

根据 Mac OS X 上的 test(1) 手册页和 FreeBSD(请注意,此警告可能已过时;它 1996 年首次出现在 NetBSD):

     -h file       True if file exists and is a symbolic link.  This operator
                   is retained for compatibility with previous versions of
                   this program. Do not rely on its existence; use -L instead.

显然,某些版本的 Solaris test 仅支持 -h,并且(早在 2003 年)出于兼容性原因,某些软件已切换为 -h,因此 -h 实际上可能是你最好的选择。

It appears that they both exist for legacy reasons, to be compatible between different versions of Unix. You should be able to use either one, as they do the exact same thing, but be aware that if the system you are running on is not compliant with the latest standards, it may be missing one or the other.

Both forms are present in the Single Unix Specification version 3/POSIX 2004, with no caveats:

-h  pathname
True if pathname resolves to a file that exists and is a symbolic link. False if pathname cannot be resolved, or
if pathname resolves to a file that exists but is not a symbolic link. If the final component of pathname is a
symlink, that symlink is not followed.
-L  pathname
True if pathname resolves to a file that exists and is a symbolic link. False if pathname cannot be resolved, or
if pathname resolves to a file that exists but is not a symbolic link. If the final component of pathname is a
symlink, that symlink is not followed.

According to the test(1) man page on Mac OS X and FreeBSD (note that this warning may be outdated; it first appeared in NetBSD in 1996):

     -h file       True if file exists and is a symbolic link.  This operator
                   is retained for compatibility with previous versions of
                   this program. Do not rely on its existence; use -L instead.

And apparently, some versions of Solaris test only support -h, and (back in 2003) some software has switched to -h for compatibility reasons, so -h may actually be your best bet.

来世叙缘 2024-08-24 02:04:21

没有什么区别,它们完全一样。它们的存在可能是为了统一 POSIX 之前的不同测试实现。

There is no difference, they are exactly the same. They probably exist to unify different test implementations pre-POSIX.

乖乖兔^ω^ 2024-08-24 02:04:21

Fedora 的手册页显示

   -h FILE
          FILE exists and is a symbolic link (same as -L)

Fedora's man page says

   -h FILE
          FILE exists and is a symbolic link (same as -L)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文