测试-h和测试-L之间的区别

Sac*_*iya 11 testing shell symlink ksh

test -L filenametest -h filenameksh 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.
Run Code Online (Sandbox Code Playgroud)

Nor*_*sey 18

源代码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));
Run Code Online (Sandbox Code Playgroud)

从这一点我得出结论,他们的行为完全相同,但这-h是一个遗留选项,可能有一天会消失:-)

  • 我很欣赏回答我的问题的方式。 (2认同)

Bri*_*ell 9

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

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

-h  pathname
如果pathname解析为存在且是符号链接的文件,则为True .假,如果路径不能得到解决,或者如果路径解析到一个真实存在,但不是一个符号链接的文件.如果pathname的最后一个组件是符号链接,则不遵循该符号链接.
-L  路径名
如果pathname解析为存在且是符号链接的文件,则为True .假,如果路径不能得到解决,或者如果路径解析到一个真实存在,但不是一个符号链接的文件.如果pathname的最后一个组件是符号链接,则不遵循该符号链接.

根据test(1)Mac OS X和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.

显然,某些版本的Solaristest仅支持-h,并且(早在2003年)由于兼容性原因,某些软件已经切换到-h了,所以-h实际上可能是你最好的选择.