你好,游客 登录 注册 搜索
背景:
阅读新闻

rpm批量删除安装包

[日期:2012-08-22] 来源:Linux社区  作者:topurce [字体:大 中 小]

这几天在安装一些软件时,版本总有这样或那样的问题,这就是开源的痛苦之处,总是有版本的问题,很难像windows那样可以很好的兼容,所以常常需要安装和卸载,有时安装了一系列相关的rpm包,卸载却只能一个一个的卸,很烦,所以自己写了个批处理,感觉还蛮好用的,现共享给大家:

#!/bin/bash

################################################################
#
#  function: batch uninstall rpm packages
#  setup:
#         1. copy the scripts and save as a file, such as: ex.sh
#         2. switch to root user. su - root
#         3. change the file's permission: chmod +x ex.sh
#         3. running the script with no parameter: ./ex.sh
#  runing:
#        uninstall [rpm package name]
#  author: Topurce Zhou (topurce#at#hotmail.com)
#
################################################################

if [ "$UID" -ne 0 ]
then
    echo -e 'must be \E[34m\033[1mroot\033[0m to run this script.'
    echo -ne '\E[0m'
    exit 67
fi

if [ ! -f /usr/bin/uninstall ]
then
   echo "building file..."
   scripts="$(cat $0)"
   declare -i index=1
   cat $0 | while read line
   do
       if (( index == 19 ))
       then
          echo 'echo -e "must be \E[34m\033[1mroot\033[0m to run this script."'>>/usr/bin/uninstall
          echo 'echo -ne "\E[0m"'>>/usr/bin/uninstall
       elif (( index == 23 ))
       then
          echo 'stips="searching packages for \"$1\":"'>>/usr/bin/uninstall
      echo 'usage="usage: $0 \"package name\""'>>/usr/bin/uninstall
       elif (( index != 19 && index != 20 && (index<23 || index>52) ))
       then
          echo $line>>/usr/bin/uninstall
       fi
       index+=1;
   done
   chmod +x /usr/bin/uninstall
   echo "try \"uninstall [package name]\" again."
   exit
fi

stips="searching packages for \"$1\":"
usage="usage: $0 \"rpm package name\""

if [ $# -eq 0 ]
then
    echo "$0: no rpm packages given for uninstall."
    echo $usage
elif [ $# -gt 1 ]
then   
    echo $usage
else
    echo $stips
    rpms="$(rpm -qa | grep $1)"
    declare -i count=0
    for rpmk in $rpms
    do
       count+=1
       echo "package: $rpmk"
    done
    if (( count == 0 ))
    then
       echo "no packages"
       exit
    fi
    echo "packages: $count"
    echo
    read -p "are you sure you want to uninstall all above packages?(y/n)"
    if [[ $REPLY == [Yy] ]]
    then
         echo "starting to uninstall packages..."
         for rpmk in $rpms
     do
         count+=1
         echo "uninstalling package: $rpmk"
             rpm -e --nodeps $rpmk
             if [ $? -eq 0 ]
             then
             echo "done"
             else
                 echo "faild to uninstall $rpmk"
             fi
      done
    fi
fi

将上面的代码复制,然后保存到一个文件,并修改文件属性为可执行。然后以root身份执行之后,这个代码会将自己安装到/usr/bin/uninstall

这样,你就可以执行uninstall [包名]  进行批量卸载了,这里的包名是模糊查询,并且在正式卸载之前会提醒你确认的,所以多了一层保护,以免错删。

linux
相关资讯       rpm批量删除 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款