使用apt-get install命令下载安装软件时,有时候不清楚一个软件到底依赖于哪些其它软件包,操作起来比较麻烦。最近编制了一个shell脚本,专门用来解决这个问题。
安装kdbg时遇到的状况:
sudo apt-get install kdbg
[sudo] password for x:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
kdbg : Depends: kde-runtime but it is not going to be installed
Depends: libkdeui5 (>= 4:4.4.0) but it is not going to be installed
Depends: libkio5 (>= 4:4.4.0) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
使用脚本工具安装,执行一下命令安装3层依赖关系所产生的全部依赖包。
sudo ./downloadalldepends.sh 3 kdbg
自动安装成功kdbg所需的全部依赖包,最后安装了kdbg。
可以使用如下命令查询kdbg的1层依赖关系所产生的全部依赖包:
./getalldepends.sh 1 kdbg
libstdc++6 libqtgui4 libqtcore4 libkio5 libkdeui5 libkdecore5 libc6 kde-runtime kdbg
x@u:~/test/apt$
本脚本限定了最大深度为10。
所涉及到的两个脚本如下:
安装的脚本:
#!/bin/bash
########################################################################################
###FileName : downloadalldepends.sh
###Usage : downloadalldepends.sh DEPTH PKG1 PKG2 PKG3 ...
###Author : coe2coe@qq.com
###Description : Download dependencies of one or more packages with apt-get install --reinstall -d PKG.
########################################################################################
allpkgs=$(./getalldepends.sh $*)
ret=$?
if [ ${ret} -ne 0 ]
then
echo "${allpkgs}"
echo "FAILED to getalldepends:{${ret}}."
exit ${ret}
fi
echo "ALL DEPENDENCIES:"
echo "================="
echo "${allpkgs}"
echo "=================="
sudo apt-get -y install ${allpkgs}
查询的脚本:
1 #!/bin/bash
2
3
4 ########################################################################################
5 ###FileName : getalldepends.sh
6 ###Usage : getalldepends.sh DEPTH PKG1 PKG2 PKG3 ...
7 ###Author : coe2coe@qq.com
8 ###Description : Query dependencies of one or more packages with apt-cache depends PKG.
9 ########################################################################################
10
11
12
13 MAXDEPTH=10
14 depth=$1
15 shift
16 arguments=$*
17 argument_count=$#
18 pkgs=()
19
20
21 function add_package()
22 {
23 pkgs[${#pkgs[*]}]=${1}
24 }
25
26
27 function add_more_package()
28 {
29 local t
30 for t in $@
31 do
32 add_package "${t}"
33 done
34 return 0
35 }
36
37
38 function get_depends( )
39 {
40 local i=${1}
41 local a=""
42 local b=""
43 local thispkgs=""
44
45 if [ ${i} -lt 1 ]
46 then
47 return 0
48 fi
49
50 let i--
51
52 shift
53 for p in $@ do
54 do
55 a="${a} ${p}"
56 done
57
58 b=$(apt-cache depends ${a} |grep Depends |cut -d: -f2 |xargs)
59 thispkgs=$(echo ${b} |sed -r 's/\B<.*>\B//g')
60 add_more_package ${thispkgs}
61 get_depends ${i} ${thispkgs}
62
63 return 1
64
65 }
66
67
68
69 function initialize()
70 {
71
72 if [ ${argument_count} -lt 1 ]
73 then
74 echo "ERROR:usage:getalldepends.sh DEPTH PKG1 PKG2 PKG3 ..."
75 exit -1
76 fi
77
78 expr ${depth} + 0 &> /dev/null
79 if [ $? -ne 0 ]
80 then
81 echo "ERROR: depth must be an integer between 1 and ${MAXDEPTH}."
82 exit -1
83 fi
84
85 if [ ${depth} -lt 1 ] || [ ${depth} -gt ${MAXDEPTH} ]
86 then
87 echo "ERROR: depth must be between 1 and ${MAXDEPTH}."
88 exit -3
89 fi
90
91 add_more_package ${arguments}
92 get_depends ${depth} ${arguments}
93
94 }
95
96
97 function do_reverse()
98 {
99 local j=0
100 local x=""
101 local cnt=${#pkgs[*]}
102
103 while [ ${j} -lt $((${cnt}/2)) ]
104 do
105 x=${pkgs[${j}]}
106 pkgs[${j}]=${pkgs[${cnt} - 1 - ${j} ]}
107 pkgs[ ${cnt} - 1 - ${j} ]=${x}
108 ((j++))
109 done
110 }
111
112 function output()
113 {
114 echo ${pkgs[*]}
115 }
116
117
118
119 function run()
120 {
121 initialize
122 do_reverse
123 output
124 }
125
126
127 run
这个脚本目前还不是足够的智能化,在依赖包的版本与系统中现存的包的版本存在差异时,不能自动删除系统中的版本。有时候版本不同导致apt-get install发生错误。此时需要人工介入,执行apt-get remove删除掉系统中的版本,然后再执行downloadalldependencies.sh脚本即可成功安装。