怎样使用rpm命令一次性删除依赖的软件包
搜索了一下网络,发现解法大体有两种:
第一种方法:不管依赖包
相互依赖的软件包,使用rpm的--nodeps参数就搞定了.
rpm --nodeps -e gdm-2.24.1-4.fc10.i386
Don’t check dependencies before uninstalling the packages.
也就是说不检查依赖。这样的话,那些使用该软件包的软件在此之后可能就不能正常工作了。
第二种方法:手工添加依赖包到命令行
执行
这种方法是我以前也经常使用的,比如用于删除RHEL/CentOS中原装的jdk
下面演示这一步骤,比较烦,因为要删除的依赖包实在太多,要复制粘贴n次
[root@localhost ~]#java -version
java version "1.6.0_22" OpenJDK Runtime Environment (IcedTea6 1.10.4) (rhel-1.24.1.10.4.el5-i386) OpenJDK Server VM (build 20.0-b11,mixed mode)
[root@localhost ~]#rpm -qa | grep jdk
java-1.6.0-openjdk-1.6.0.0-1.24.1.10.4.el5 java-1.6.0-openjdk-devel-1.6.0.0-1.24.1.10.4.el5
[root@localhost ~]#rpm -e java-1.6.0-openjdk-1.6.0.0-1.24.1.10.4.el5 java-1.6.0-openjdk-devel-1.6.0.0-1.24.1.10.4.el5
error: Failed dependencies: jre >= 1.5.0 is needed by (installed) openoffice.org-ure-3.1.1-19.5.el5_5.6.i386
[root@localhost ~]#rpm -e java-1.6.0-openjdk-1.6.0.0-1.24.1.10.4.el5 java-1.6.0-openjdk-devel-1.6.0.0-1.24.1.10.4.el5 openoffice.org-ure-3.1.1-19.5.el5_5.6.i386error: Failed dependencies: libjvmaccessgcc3.so.3 is needed by (installed) openoffice.org-core-3.1.1-19.5.el5_5.6.i386 libjvmaccessgcc3.so.3(UDK_3.1) is needed by (installed) openoffice.org-core-3.1.1-19.5.el5_5.6.i386
太多输出,省略
by (installed) openoffice.org-calc-3.1.1-19.5.el5_5.6.i386 openoffice.org-ure = 1:3.1.1-19.5.el5_5.6 is needed by (installed) openoffice.org-graphicfilter-3.1.1-19.5.el5_5.6.i386 openoffice.org-ure = 1:3.1.1-19.5.el5_5.6 is needed by (installed) openoffice.org-draw-3.1.1-19.5.el5_5.6.i386
[root@localhost ~]#有太多软件包需要删除,此处不再继续
[root@localhost ~]#
第三种方法:用脚本(当然:有可能写脚本比复制粘贴更难,前者是智力活,后者是体力活)
编写一个force_remove_package.sh 的Bash脚本,内容如下:
- #!/bin/sh
- do_once()
- {
- rpm-e"$@"2>&1|grep'(installed)'
- }
- for((I=1;I<=4;++I))
- do
- DEPS="$DEPS$(do_once"$@"$DEPS|awk'{print$8}')"
- echo$I$DEPS
- done
其中,
1. 用 for 循环进行有限次尝试 而不用 while true,那是为了防止编程死循环,别因为输入错误真的把系统里面所有的包都给删除了;
2. awk命令里面的 $8,是经过尝试出来的,因为 rpm -e 命令输出的信息中包含有很多空格;
3. rpm -e 的错误输出需要重定向到标准输出,否则就不会得到依赖包,而直接输出在终端上了。
[root@localhost setup]#./force_remove_package.sh java-1.6.0-openjdk-1.6.0.0-1.24.1.10.4.el5 java-1.6.0-openjdk-devel-1.6.0.0-1.24.1.10.4.el5
1 openoffice.org-ure-3.1.1-19.5.el5_5.6.i386
2 openoffice.org-ure-3.1.1-19.5.el5_5.6.i386 openoffice.org-core-3.1.1-19.5.el5_5.6.i386 openoffice.org-writer-3.1.1-19.5.el5_5.6.i386 openoffice.org-impress-3.1.1-19.5.el5_5.6.i386 openoffice.org-math-3.1.1-19.5.el5_5.6.i386 openoffice.org-calc-3.1.1-19.5.el5_5.6.i386 openoffice.org-graphicfilter-3.1.1-19.5.el5_5.6.i386 openoffice.org-draw-3.1.1-19.5.el5_5.6.i386
3 openoffice.org-ure-3.1.1-19.5.el5_5.6.i386 openoffice.org-core-3.1.1-19.5.el5_5.6.i386 openoffice.org-writer-3.1.1-19.5.el5_5.6.i386 openoffice.org-impress-3.1.1-19.5.el5_5.6.i386 openoffice.org-math-3.1.1-19.5.el5_5.6.i386 openoffice.org-calc-3.1.1-19.5.el5_5.6.i386 openoffice.org-graphicfilter-3.1.1-19.5.el5_5.6.i386 openoffice.org-draw-3.1.1-19.5.el5_5.6.i386 openoffice.org-xsltfilter-3.1.1-19.5.el5_5.6.i386 openoffice.org-langpack-zh_TW-3.1.1-19.5.el5_5.6.i386 openoffice.org-langpack-zh_CN-3.1.1-19.5.el5_5.6.i386
4 openoffice.org-ure-3.1.1-19.5.el5_5.6.i386 openoffice.org-core-3.1.1-19.5.el5_5.6.i386 openoffice.org-writer-3.1.1-19.5.el5_5.6.i386 openoffice.org-impress-3.1.1-19.5.el5_5.6.i386 openoffice.org-math-3.1.1-19.5.el5_5.6.i386 openoffice.org-calc-3.1.1-19.5.el5_5.6.i386 openoffice.org-graphicfilter-3.1.1-19.5.el5_5.6.i386 openoffice.org-draw-3.1.1-19.5.el5_5.6.i386 openoffice.org-xsltfilter-3.1.1-19.5.el5_5.6.i386 openoffice.org-langpack-zh_TW-3.1.1-19.5.el5_5.6.i386 openoffice.org-langpack-zh_CN-3.1.1-19.5.el5_5.6.i386
[root@localhost setup]#
当然,还可以继续改进,如下:
- #!/bin/sh
- do_once()
- {
- rpm-e"$@"2>&1|grep'(installed)'
- }
- for((I=1;I<=5;++I))
- do
- NEWDEPS="$(do_once"$@"$DEPS|awk'{print$6}')"
- DEPS="$DEPS$NEWDEPS"
- echo$I$NEWDEPS
- if[!"$NEWDEPS"];then
- break
- fi
- done
这个改进主要是,
1. 对每次检查时新增的依赖包进行检查,如果没有新增的依赖包($NEWDEPS),程序停止运行;
2. awk 输出的 $8 也变成了 $6。其中缘由现在还没有找到。
用它来删除tomcat试试。
[root@localhost setup]#rpm -qa | grep tomcat
tomcat5-servlet-2.4-api-5.5.23-0jpp.22.el5_7
tomcat5-jsp-2.0-api-5.5.23-0jpp.22.el5_7
[root@localhost setup]#./force_remove_package.sh tomcat5-servlet-2.4-api-5.5.23-0jpp.22.el5_7 tomcat5-jsp-2.0-api-5.5.23-0jpp.22.el5_7
1 hsqldb-1.8.0.9-1jpp.2.i386 bsf-2.3.0-11jpp.1.i386 bsf-2.3.0-11jpp.1.i386
2 bsh-1.3.0-9jpp.1.i386
3
[root@localhost setup]#rpm -qa | grep tomcat
[root@localhost setup]#
发现上面改进过的脚本还是有问题的,在删除带有java的软件包时会有问题
改用sed而不是awk
- #!/bin/sh
- do_once()
- {
- rpm-e"$@"
- rpm-e"$@"2>&1|grep'(installed)'
- }
- for((I=1;I<=5;++I))
- do
- #NEWDEPS="$(do_once"$@"$DEPS|awk'{print$6$8}')"
- NEWDEPS="$(do_once"$@"$DEPS|sed-e's/^.*(installed)//')"
- DEPS="$DEPS$NEWDEPS"
- echo$I$NEWDEPS
- if[!"$NEWDEPS"];then
- break
- fi
- done
使用sed替换能准确的取得依赖的软件包名称。
[root@localhost setup]#rpm -qa | grep java
java-1.4.2-gcj-compat-1.4.2.0-40jpp.115
tzdata-java-2011l-4.el5
[root@localhost setup]#
[root@localhost setup]#./force_remove_package.sh java-1.4.2-gcj-compat-1.4.2.0-40jpp.115 tzdata-java-2011l-4.el5 1 xml-commons-apis-1.3.02-0.b2.7jpp.10.i386 xerces-j2-2.7.1-7jpp.2.el5_4.2.i386 xalan-j2-2.7.0-6jpp.1.i386 xml-commons-resolver-1.1-1jpp.12.i386 gjdoc-0.7.7-12.el5.i386 antlr-2.7.6-4jpp.2.i386 lucene-1.4.3-1jpp.16.i386 2 [root@localhost setup]# rpm -qa | grep java [root@localhost setup]#