如何检查是否存在符号链接 [英] How to check if symlink exists
问题描述
我想检查是否在bash存在一个符号链接。下面是我试过。
I'm trying to check if a symlink exists in bash. Here's what I've tried.
mda=/usr/mda
if [ ! -L $mda ]; then
echo "=> File doesn't exist"
fi
mda='/usr/mda'
if [ ! -L $mda ]; then
echo "=> File doesn't exist"
fi
然而,这是行不通的。
如果!被冷落,它永远不会触发。而如果 '!'在那里,它触发每一次。
However, that doesn't work. If '!' is left out, it never triggers. And if '!' is there, it triggers every time.
推荐答案
-L
如果文件的存在,是一个符号链接(链接的文件可能返回true或可能不存在)。你想 -f
或者只是 -e
(返回true(如果文件存在且是一个普通文件返回true)不论哪种类型,如果文件存在)。
-L
returns true if the "file" exists and is a symbolic link (the linked file may or may not exist). You want -f
(returns true if file exists and is a regular file) or maybe just -e
(returns true if file exists regardless of type).
按照 GNU手册页, -h
等同于 -L
,但根据 BSD手册页< /一>,它不应该被用于:
According to the GNU manpage, -h
is identical to -L
, but according to the BSD manpage, it should not be used:
-h文件,如果文件存在且是一个符号链接则为真。该运算符保留这个节目的previous版本的兼容性。不要依赖于它的存在;使用-L来代替。
这篇关于如何检查是否存在符号链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!