专注各种脚本编程
Baidu
加入收藏夹
本站内容有下面分类知识,欢迎您的到来^_^
shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言 oracle
当前位置:| 主页>shell脚本欣赏>

一个删除无效链接的shell脚本

百度收藏 QQ搜藏

感谢作者: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脚本 下一篇:两个只有一行的shell脚本

power by soyo123 2007-2008