删除无效链接的shell脚本
上一篇 /
下一篇 2008-04-11 21:42:03
/ 个人分类:Shell
感谢作者:home_king兄
说明
1.专家模式(删除前免提示),且进入专家模式前作出提示
2.仅作用于一层目录的功能选项
3.该脚本利用find的目录递归功能删除系统上无效(目标文件不存在)的软链接
代码:
#!/bin/sh
# When lpath is /, it can delete all null links existing in your system INTERACTIVELY!!!
#
# Written by home_king<home_king@163.com>
#
prompthelp()
{
echo ========================================
echo 'Delete symbolic links with null target.'
echo 'usage:dellink [-e] [-o] [PATH]'
echo ' -f:EXPERT mode,no prompt.WARNNING!!!'
echo ' -c:Just apply to current directory.'
echo ' -h:Print this help.'
echo ' Without PATH, we set it ".".'
echo ========================================
}
delflag=""
promptdel()
{
read -p 'WARNNING!!!Without PROMPT!!!Continue?[y/n]' delflag
case $delflag in
y ) return 0;;
n ) exit 1;;
* ) promptdel
esac
}
while getopts ":fch" opt; do
case $opt in
f ) INTERACTIVE="f"
promptdel;;
c ) DEPTH="maxdepth 1";;
h ) prompthelp
exit 0;;
? ) echo "Invalid Option!"
prompthelp
exit 1
esac
done
shift $(($OPTIND - 1))
lpath=$1
[ $# -gt 1 ] && prompthelp && exit 1
if [ -d $1 ]; then
linklist=$(find $1 -${DEPTH:-"depth"} -type l |xargs)
for i in $linklist; do
[ ! -f $i ] && rm -${INTERACTIVE:-"i"} $i
done
else
echo "PATH is not a directory!"
prompthelp
fi
相关阅读:
- 一个 Shell 程序的性能优化 (ainux, 2008-4-11)
- 更多的 bash 基本编程 (ainux, 2008-4-11)
- Linux解释器原理 (ainux, 2008-4-11)
- 在Linux中批量建立用户的shell (ainux, 2008-4-11)
- 基于图形界面的改变系统启动运行等级shell脚本 (ainux, 2008-4-11)
- 一个用mplayer听音乐的简单脚本 (ainux, 2008-4-11)
- 一个利用suspend控制终端的shell脚本 (ainux, 2008-4-11)
- 病毒研究之感染linux脚本程序技术 (ainux, 2008-4-11)
- shell版俄罗斯方块游戏 (ainux, 2008-4-11)
- 文件中两行互换的shell脚本 (ainux, 2008-4-11)
收藏
举报
TAG:
Shell