歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> rpm批量刪除安裝包

rpm批量刪除安裝包

日期:2017/2/28 15:42:40   编辑:Linux教程

這幾天在安裝一些軟件時,版本總有這樣或那樣的問題,這就是開源的痛苦之處,總是有版本的問題,很難像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 [包名] 進行批量卸載了,這裡的包名是模糊查詢,並且在正式卸載之前會提醒你確認的,所以多了一層保護,以免錯刪。

Copyright © Linux教程網 All Rights Reserved