以下脚本是一本《linux命令、编辑器与shell编程》的书上的,是美国的Mark G Sobell著,脚本的功能是查找某个文件在指定的目录是否有硬连接。但是这个脚本有个小bug,只要你细心,应该可以发现。把你的结果跟帖给我吧。
#!/bin/bash
#Identify links to a file
#Usage: lnks file [directory]
if [ $# -eq 0 -o $# -gt 2 ] ;
then
echo "Usage; lnks file [directory]" 1>&2
exit
fi
#Identify links to a file
#Usage: lnks file [directory]
if [ $# -eq 0 -o $# -gt 2 ] ;
then
echo "Usage; lnks file [directory]" 1>&2
exit
fi
if [ -d "$1" ];
then
echo "First argument cannot be a directory." 1>&2
echo "Usage: lnks file [directory]" 1>&2
exit
else
file="$1"
fi
then
echo "First argument cannot be a directory." 1>&2
echo "Usage: lnks file [directory]" 1>&2
exit
else
file="$1"
fi
if [ $# -eq 1 ] ;
then
directory="."
elif [ -d $2 ] ;
then
directory="$2"
else
echo "Optional second argument must be a directory." 1>&2
echo "Usage:lnks file [directory]" 1>&2
exit
fi
then
directory="."
elif [ -d $2 ] ;
then
directory="$2"
else
echo "Optional second argument must be a directory." 1>&2
echo "Usage:lnks file [directory]" 1>&2
exit
fi
if [ ! -f "$file" ] ;
then
echo "lnks: $file not found or special file" 1>&2
exit
fi
then
echo "lnks: $file not found or special file" 1>&2
exit
fi
set -- $(ls -l "$file")
linkcnt=$2
if [ "$linkcnt" -eq 1 ];
then
echo "Lnks: no other hard links to $file" 1>&2
exit
fi
linkcnt=$2
if [ "$linkcnt" -eq 1 ];
then
echo "Lnks: no other hard links to $file" 1>&2
exit
fi
set $(ls -i "file")
inode=$1
echo "lnks:using find to search for links..." 1>&2
find "$directory" -xdev -inum $inode -print
inode=$1
echo "lnks:using find to search for links..." 1>&2
find "$directory" -xdev -inum $inode -print
本文转自 fenghao.cn 51CTO博客,原文链接:http://blog.51cto.com/linuxguest/132646,如需转载请自行联系原作者