bash 如何检查符号链接是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5767062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a symlink exists
提问by bear
I'm trying to check if a symlink exists in bash. Here's what I've tried.
我正在尝试检查 bash 中是否存在符号链接。这是我尝试过的。
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.
但是,这不起作用。如果 '!' 被排除在外,它永远不会触发。而如果 '!' 在那里,它每次都会触发。
回答by drysdam
-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).
-L
如果“文件”存在并且是符号链接(链接文件可能存在也可能不存在),则返回真。你想要-f
(如果文件存在并且是一个普通文件则-e
返回真)或者可能只是(如果文件存在则返回真而不管类型如何)。
According to the GNU manpage, -h
is identical to -L
, but according to the BSD manpage, it should not be used:
根据GNU 手册页,-h
与 相同-L
,但根据BSD 手册页,不应使用:
-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.
-h file
如果文件存在并且是符号链接,则为真。保留此运算符是为了与此程序的先前版本兼容。不要依赖它的存在;使用 -L 代替。
回答by Corin
-L is the test for file exists and is alsoa symbolic link
-L 是文件是否存在的测试,也是一个符号链接
If you do not want to test for the file being a symbolic link, but just test to see if it exists regardless of type (file, directory, socket etc) then use -e
如果您不想测试该文件是否为符号链接,而只想测试它是否存在,而不管类型(文件、目录、套接字等)如何,请使用 -e
So if file is really file and not just a symbolic link you can do all these tests and get an exit status whose value indicates the error condition.
因此,如果文件真的是文件而不仅仅是符号链接,您可以执行所有这些测试并获得退出状态,其值指示错误条件。
if [ ! \( -e "${file}" \) ]
then
echo "%ERROR: file ${file} does not exist!" >&2
exit 1
elif [ ! \( -f "${file}" \) ]
then
echo "%ERROR: ${file} is not a file!" >&2
exit 2
elif [ ! \( -r "${file}" \) ]
then
echo "%ERROR: file ${file} is not readable!" >&2
exit 3
elif [ ! \( -s "${file}" \) ]
then
echo "%ERROR: file ${file} is empty!" >&2
exit 4
fi
回答by Chen Levy
You can check the existence of a symlink and that it is not broken with:
您可以检查符号链接是否存在并且它没有被破坏:
[ -L ${my_link} ] && [ -e ${my_link} ]
So, the complete solution is:
所以,完整的解决方案是:
if [ -L ${my_link} ] ; then
if [ -e ${my_link} ] ; then
echo "Good link"
else
echo "Broken link"
fi
elif [ -e ${my_link} ] ; then
echo "Not a link"
else
echo "Missing"
fi
回答by Lynch
Maybe this is what you are looking for. To check if a file exist and is not a link.
也许这就是你正在寻找的。检查文件是否存在并且不是链接。
Try this command:
试试这个命令:
file="/usr/mda"
[ -f $file ] && [ ! -L $file ] && echo "$file exists and is not a symlink"
回答by popedotninja
How about using readlink
?
怎么用readlink
?
# if symlink, readlink returns not empty string (the symlink target)
# if string is not empty, test exits w/ 0 (normal)
#
# if non symlink, readlink returns empty string
# if string is empty, test exits w/ 1 (error)
simlink? () {
test "$(readlink "")";
}
FILE=/usr/mda
if simlink? "${FILE}"; then
echo $FILE is a symlink
else
echo $FILE is not a symlink
fi
回答by DigitalRoss
Is the file really a symbolic link? If not, the usual test for existence is -r
or -e
.
该文件真的是一个符号链接吗?如果不是,则通常的存在测试是-r
或-e
。
See man test
.
见man test
。
回答by Amin Shateri
first you can do with this style:
mda="/usr/mda" if [ ! -L "${mda}" ]; then echo "=> File doesn't exist" fi
if you want to do it in more advanced style you can write it like below:
#!/bin/bash mda="" if [ -e "" ]; then if [ ! -L "" ] then echo "you entry is not symlink" else echo "your entry is symlink" fi else echo "=> File doesn't exist" fi
首先你可以使用这种风格:
mda="/usr/mda" if [ ! -L "${mda}" ]; then echo "=> File doesn't exist" fi
如果你想以更高级的风格来做,你可以像下面这样写:
#!/bin/bash mda="" if [ -e "" ]; then if [ ! -L "" ] then echo "you entry is not symlink" else echo "your entry is symlink" fi else echo "=> File doesn't exist" fi
the result of above is like:
上面的结果是这样的:
root@linux:~# ./sym.sh /etc/passwd
you entry is not symlink
root@linux:~# ./sym.sh /usr/mda
your entry is symlink
root@linux:~# ./sym.sh
=> File doesn't exist
回答by Andrew Lazarus
If you are testing for file existence you want -e not -L. -L tests for a symlink.
如果您正在测试文件是否存在,则需要 -e 而不是 -L。-L 测试符号链接。