代码之家  ›  专栏  ›  技术社区  ›  Sachin

试验-H和试验-L之间的差异

  •  10
  • Sachin  · 技术社区  · 16 年前

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

    这是手册页上的描述。

     -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 回复  |  直到 16 年前
        1
  •  18
  •   Norman Ramsey    16 年前

    的源代码 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));
    

    由此我得出结论,他们的行为完全相同,但是 -h 是一个传统的选择,也许有一天会消失:-)

        2
  •  9
  •   Brian Campbell Dennis Williamson    16 年前

    它们的存在似乎都是由于遗留的原因,以便在不同版本的unix之间兼容。您应该能够使用其中任何一个,因为他们做了完全相同的事情,但要注意,如果您运行的系统不符合最新的标准,它可能缺少一个或另一个。

    两种形式都出现在 Single Unix Specification version 3/POSIX 2004 ,无需注意:

    -H 路径名
    如果真 路径名 解析为存在且是符号链接的文件。假若 路径名 无法解决,或 如果 路径名 解析为存在但不是符号链接的文件。如果 路径名 是一个 symlink,这个symlink不在后面。
    -L 路径名
    如果真 路径名 解析为存在且是符号链接的文件。假若 路径名 无法解决,或 如果 路径名 解析为存在但不是符号链接的文件。如果 路径名 是一个 symlink,这个symlink不在后面。

    根据 test(1) Mac OS X和 FreeBSD (请注意,此警告可能已过时;它 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.
    

    显然, some versions of Solaris test 仅支持 -h ,和(早在2003年) some software has switched to -h 出于兼容性的原因,所以 -H 可能是你最好的选择。

        3
  •  2
  •   Nietzche-jou    16 年前

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

        4
  •  1
  •   wallyk    16 年前

    Fedora的手册上说

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