检查文件是否是 Linux 上的链接

发布于 2024-07-16 16:09:38 字数 154 浏览 6 评论 0原文

我正在尝试使用 -h filename 命令检查 KornShell (ksh) 脚本中是否存在符号链接。 这在 HP 盒子上效果很好。

不确定 Linux、AIX 和 Solaris 机器的正确选项是什么。

对此有什么见解吗?

I am trying to check if a symbolic link exists from a KornShell (ksh) script using -h filename command. This works good on HP boxes.

Not sure what is the correct option for Linux, AIX and Solaris boxes.

Any insight regarding this?

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

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

发布评论

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

评论(4

-hPOSIX 规范 的一部分; 它应该在任何模糊合理的地方都有效。

根据 Mac OS X 上的 man test

     -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.

-L 也是标准化的,因此,如果您发现 -h 不起作用,您应该尝试使用 -L 代替。

-h is part of the POSIX spec; it should work everywhere that is vaguely reasonable.

According to man test on Mac OS X:

     -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.

-L is also standardized, so if you find anywhere that -h does not work, you should try -L instead.

喜爱皱眉﹌ 2024-07-23 16:09:38

至少在 Linux 系统上也是 -h(bash 是我的 shell):

lrwxrwxrwx 1 mule mule 37 Feb 27 09:43 mule.log -> /home/mule/runtime/mule/logs/mule.log
[mule@emdlcis01 ~]$ if [[ -h mule.log ]]; then echo "mule.log is a symlink that exists" ; fi
mule.log is a symlink that exists

检查 man test 以查看在给定环境中可用于文件和字符串的可用运算符。

It is -h on linux systems also, at least (bash is my shell):

lrwxrwxrwx 1 mule mule 37 Feb 27 09:43 mule.log -> /home/mule/runtime/mule/logs/mule.log
[mule@emdlcis01 ~]$ if [[ -h mule.log ]]; then echo "mule.log is a symlink that exists" ; fi
mule.log is a symlink that exists

Check man test to see the available operators you have available to use on files and strings in your given enviorment.

白况 2024-07-23 16:09:38

最好的答案是在您的系统上尝试“测试”手册页中所说的任何内容。 如果这看起来有效,那就别再犹豫了。 但是,如果它似乎不起作用,或者您对要测试的更晦涩的选项有疑问,那么您还应该检查 shell 的手册页,看看“expr”或“[”是否是内置的。 在这种情况下,shell 可能使用内部实现,而不是从 /bin 调用 expr 实用程序。 在 Solaris 上,我验证了 ksh93 将 [ 视为内置函数(尽管手册页似乎没有这么说)。 从 truss 输出中,您可以看到 ksh 没有运行 [ 的 expr 命令。

% truss -f -texec /bin/ksh '[ -h /home ]'
26056:  execve("/usr/bin/ksh", 0x08047708, 0x08047714)  argc = 2
26056:  execve("/usr/bin/ksh93", 0x08047708, 0x08047714)  argc = 2
26056:  execve("/usr/bin/amd64/ksh93", 0x08047704, 0x08047710)  argc = 2

% truss -f -texec /bin/ksh '/bin/expr -h /home ]'
26058:  execve("/usr/bin/ksh", 0x08047700, 0x0804770C)  argc = 2
26058:  execve("/usr/bin/ksh93", 0x08047700, 0x0804770C)  argc = 2
26058:  execve("/usr/bin/amd64/ksh93", 0x080476FC, 0x08047708)  argc = 2
26058:  execve("/bin/expr", 0x00418360, 0x00418398)  argc = 4

The best answer is to try whatever the 'test' man page says on your system. If that seems to work, look no further. However, if it doesn't seem to work, or if you have questions about more obscure options to test, then you should also check the man page for your shell to see if 'expr' or '[' are built-ins. In that case, the shell might be using an internal implementation instead of calling the expr utility from /bin. On Solaris I verified that ksh93 treats [ as a builtin (even though the man page doesn't seem to say so). From the truss output you can see that ksh is not running the expr command for [.

% truss -f -texec /bin/ksh '[ -h /home ]'
26056:  execve("/usr/bin/ksh", 0x08047708, 0x08047714)  argc = 2
26056:  execve("/usr/bin/ksh93", 0x08047708, 0x08047714)  argc = 2
26056:  execve("/usr/bin/amd64/ksh93", 0x08047704, 0x08047710)  argc = 2

% truss -f -texec /bin/ksh '/bin/expr -h /home ]'
26058:  execve("/usr/bin/ksh", 0x08047700, 0x0804770C)  argc = 2
26058:  execve("/usr/bin/ksh93", 0x08047700, 0x0804770C)  argc = 2
26058:  execve("/usr/bin/amd64/ksh93", 0x080476FC, 0x08047708)  argc = 2
26058:  execve("/bin/expr", 0x00418360, 0x00418398)  argc = 4
吻风 2024-07-23 16:09:38

两个可能的选项是

    if [ -h filename ] 
OR
    ls -ltr | grep filename | grep ^l

If $? 是 0 则文件被链接,否则文件未链接,我更喜欢第一个选项。

Thw two possible options are

    if [ -h filename ] 
OR
    ls -ltr | grep filename | grep ^l

If $? is 0 then file is linked otherwise its not linked, I will prefer the first option instead.

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