本文介绍了Symlink检查-Linux Bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个脚本,该脚本在目录中进行搜索以找到指向不存在的对象的符号链接.
I'm trying to create a script that searches through a directory to find symlinks that point to non-existing objects.
我的目录中有一个文件,该文件的符号链接已删除,但是由于某些原因,当我运行以下脚本时,它说文件存在.
I have a file in a directory with a deleted symlink, but for some reason when i run the below script It says file exists.
#!/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
运算符检查存在性.
Check this page. It has a test for broken links. It uses the -h
operator to identify a symlink and the -e
operator to check existance.
在该页面上:
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 $?
这篇关于Symlink检查-Linux Bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!