繁体   English   中英

Symlink检查-Linux Bash脚本

[英]Symlink check - Linux Bash Script

我正在尝试创建一个脚本,该脚本在目录中进行搜索以找到指向不存在的对象的符号链接。

我的目录中有一个文件,该文件的符号链接已删除,但是由于某些原因,当我运行以下脚本时,它说文件存在。

#!/bin/bash
ls -l $1 |
if [ -d $1 ]
  then
    while read file
    do
            if test -e $1
            then
                    echo "file exists"
            else
                    echo "file does not exist"
            fi
    done
 else
    echo "No directory given"
fi

谢谢

检查此页面 它对断开的链接进行测试。 它使用-h运算符标识符号链接,并使用-e运算符检查存在性。

从该页面:

linkchk () {
    for element in $1/*; do
      [ -h "$element" -a ! -e "$element" ] && echo \"$element\"
      [ -d "$element" ] && linkchk $element
    # Of course, '-h' tests for symbolic link, '-d' for directory.
    done
}

#  Send each arg that was passed to the script to the linkchk() function
#+ if it is a valid directoy.  If not, then print the error message
#+ and usage info.
##################
for directory in $directorys; do
    if [ -d $directory ]
    then linkchk $directory
    else 
        echo "$directory is not a directory"
        echo "Usage: $0 dir1 dir2 ..."
    fi
done

exit $?

您可以使用以下方法测试链接是否有效:

[[ -f "$link" ]] && echo "points to a valid file"

要检查是否确实是链接,请使用-L

[[ -L "$link" ]] && echo "it's a link"

似乎有一个名为symlinks的程序,除其他功能外,它symlinks满足您的需求。

暂无
暂无

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

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