如何检查符号链接是否存在 [英] How to check if a symlink exists

查看:103
本文介绍了如何检查符号链接是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查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(如果文件存在并且是常规文件,则返回true),或者只是-e(如果文件存在而与类型无关,则返回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 file如果文件存在且为符号链接,则为true.保留该运算符是为了与该程序的早期版本兼容.不要依靠它的存在;改用-L.

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

这篇关于如何检查符号链接是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆