简体   繁体   English

如何检查符号链接是否存在

[英]How to check if a symlink exists

I'm trying to check if a symlink exists in bash.我正在尝试检查 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 returns true if the "file" exists and is a symbolic link (the linked file may or may not exist). -L如果“文件”存在并且是一个符号链接(链接的文件可能存在也可能不存在),则返回真。 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).你想要-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. -h file如果文件存在并且是符号链接则为真。 This operator is retained for compatibility with previous versions of this program.保留此运算符是为了与此程序的先前版本兼容。 Do not rely on its existence;不要依赖它的存在; use -L instead.使用 -L 代替。

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

-L tests whether there is a symlink, broken or not. -L测试是否存在符号链接,无论是否损坏。 By combining with -e you can test whether the link is valid (links to a directory or file), not just whether it exists.通过与-e结合,您可以测试链接是否有效(链接到目录或文件),而不仅仅是它是否存在。

-L is the test for file exists and is also a 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

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"

How about using readlink ?使用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 "${1}")";
}

FILE=/usr/mda

if simlink? "${FILE}"; then
  echo $FILE is a symlink
else
  echo $FILE is not a symlink
fi
  1. first you can do with this style:首先你可以使用这种风格:

     mda="/usr/mda" if [ ! -L "${mda}" ]; then echo "=> File doesn't exist" fi
  2. if you want to do it in more advanced style you can write it like below:如果你想以更高级的风格来做,你可以像下面这样写:

     #!/bin/bash mda="$1" if [ -e "$1" ]; then if [ ! -L "$1" ] 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

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

If you are testing for file existence you want -e not -L.如果您正在测试文件是否存在,则需要 -e 而不是 -L。 -L tests for a symlink. -L 测试符号链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM