centos 批量删除软件

1、uninstall.sh

#!/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

备注:

  1、执行批处理一定要明确你需要删除的包,很有可能模糊匹配上了一些系统包,不然会引起严重的后果;

  2、文件创建之后,修改权限 chmod +x uninstall.sh

  3、需root用户执行此命令,./uninstall.sh java

  4、分析脚本分先,本质上面,还是用rpm -qa搜索匹配的包,用rpm -e --nodeps 删除包

  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值