分享一个下载apt软件包依赖的shell脚本工具,安装顺序处理的有点缺陷,需要自己手动尝试改动一下。
#!/bin/bash
basepath=$(cd $(dirname $0);pwd)
targetpath=${basepath}/debs
mkdir -p ${targetpath}
logfile=${basepath}/download-deb-dependency.log
echo > $logfile
ret=""
function getDepends()
{
echo "fileName is" $1>>$logfile
# use tr to del < >
ret=`apt-cache depends $1|grep Depends |cut -d: -f2 |tr -d "<>"`
echo $ret|tee -a $logfile
}
function getInstallOrders()
{
declare -a list=($(<${basepath}/download-order.txt))
rm -f ${basepath}/install-order.txt
touch ${basepath}/install-order.txt
for i in "${!list[@]}";
do
printf "%s\t%s\n" "$i" "${list[$i]}"
debfile=$(ls ${basepath}/debs|grep "${list[$i]}"|head -n 1)
if [ -n "${debfile}" ];then
cmd="dpkg -i ${debfile}"
bak=$(cat ${basepath}/install-order.txt)
echo "${cmd}" > ${basepath}/install-order.txt
echo "${bak}" >> ${basepath}/install-order.txt
else
echo "deb file ${list[$i]} not found."
fi
done
}
# 需要获取其所依赖包的包
all_libs="$1"
libs=$1
if [ "${libs}" = '' ];then
echo "lib name is empty!"
else
#remove old files
rm -rf ${basepath}/debs/*
# download libs dependen. deep in 3
echo "base lib name is ${libs}"
i=0
while [ $i -lt 3 ] ;
do
let i++
echo $i
# download libs
newlist=""
for j in $libs
do
if [ -n "${j}" ];then
added="$(getDepends $j)"
newlist="$newlist $added"
fi
done
libs="$newlist"
all_libs="${all_libs} ${newlist}"
done
echo "$all_libs" > ${basepath}/download-order.txt
cd ${targetpath}
for deb in $all_libs
do
echo "donwload '$deb'..."
apt-get download ${deb}
done
cd ${basepath}
getInstallOrders
fi