检查文件是否是 Linux 上的链接
我正在尝试使用 -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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
-h
是 POSIX 规范 的一部分; 它应该在任何模糊合理的地方都有效。根据 Mac OS X 上的
man test
:-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:-L
is also standardized, so if you find anywhere that-h
does not work, you should try-L
instead.至少在 Linux 系统上也是 -h(bash 是我的 shell):
检查
man test
以查看在给定环境中可用于文件和字符串的可用运算符。It is -h on linux systems also, at least (bash is my shell):
Check
man test
to see the available operators you have available to use on files and strings in your given enviorment.最好的答案是在您的系统上尝试“测试”手册页中所说的任何内容。 如果这看起来有效,那就别再犹豫了。 但是,如果它似乎不起作用,或者您对要测试的更晦涩的选项有疑问,那么您还应该检查 shell 的手册页,看看“expr”或“[”是否是内置的。 在这种情况下,shell 可能使用内部实现,而不是从 /bin 调用 expr 实用程序。 在 Solaris 上,我验证了 ksh93 将 [ 视为内置函数(尽管手册页似乎没有这么说)。 从 truss 输出中,您可以看到 ksh 没有运行 [ 的 expr 命令。
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 [.
两个可能的选项是
If $? 是 0 则文件被链接,否则文件未链接,我更喜欢第一个选项。
Thw two possible options are
If $? is 0 then file is linked otherwise its not linked, I will prefer the first option instead.