测试-h和测试-L之间的差异。

时间:2021-05-17 03:10:22

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.

ksh shell中test -L文件名和test -h文件名之间的区别是什么?从手册页,两者都被用来识别符号链接,但我想知道确切的区别。

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.

4 个解决方案

#1


16  

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:

ksh93的源代码,在文件bltins/test中。c,表明这两种选择是完全一样的,除了作者对未来的希望:

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

由此我得出结论,它们的行为完全相同,但h是一种遗产选择,可能有一天会消失:-)

#2


8  

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.

看起来它们都是出于遗留的原因而存在的,在不同版本的Unix之间是兼容的。您应该能够使用其中任何一个,因为它们做的是完全相同的事情,但是请注意,如果您正在运行的系统不符合最新的标准,它可能会丢失一个或另一个。

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

这两种形式都存在于Unix规范版本3/POSIX 2004中,没有说明:

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

根据Mac OS X和FreeBSD上的测试(1)手册页(请注意这个警告可能已经过时了;它最早出现于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.

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.

显然,某些版本的Solaris测试只支持-h,并且(在2003年)一些软件已经转换为-h,因为兼容性的原因,所以-h可能是你最好的选择。

#3


2  

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

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

#4


1  

Fedora's man page says

Fedora的手册页

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

#1


16  

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:

ksh93的源代码,在文件bltins/test中。c,表明这两种选择是完全一样的,除了作者对未来的希望:

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

由此我得出结论,它们的行为完全相同,但h是一种遗产选择,可能有一天会消失:-)

#2


8  

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.

看起来它们都是出于遗留的原因而存在的,在不同版本的Unix之间是兼容的。您应该能够使用其中任何一个,因为它们做的是完全相同的事情,但是请注意,如果您正在运行的系统不符合最新的标准,它可能会丢失一个或另一个。

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

这两种形式都存在于Unix规范版本3/POSIX 2004中,没有说明:

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

根据Mac OS X和FreeBSD上的测试(1)手册页(请注意这个警告可能已经过时了;它最早出现于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.

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.

显然,某些版本的Solaris测试只支持-h,并且(在2003年)一些软件已经转换为-h,因为兼容性的原因,所以-h可能是你最好的选择。

#3


2  

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

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

#4


1  

Fedora's man page says

Fedora的手册页

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