linux环境一键下载离线安装包及其依赖

在linux环境下载离线安装包,经常会存在安装依赖的问题,为了方便解决下载依赖的问题,通过shell脚本,根据设置的依赖深度,全部自动下载。

** 该脚本适用于Ubuntu、UOS等Debian系使用apt依赖管理工具的linux操作系统。

** Centos等Redhat系使用yum或者dnf依赖管理工具的系统,处理逻辑和此一致。

 1 #!/bin/bash
 2 # Date:2022-04-13 
 3 # Author: Create by cooper_73
 4 
 5 echo -e "`date` \nReadly to download all of $1's packages \n\
 6 \$1 is target Software name; \n\
 7 \$2 indicates the depth of the dependency level. e.g.:123...;\n\
 8 \$3 is the package architecture. e.g.:amd64、arm64..."
 9 
10 sleep 1
11 getDepends()
12 {
13   pkg_list=`apt-cache  depends $1|grep -E "依赖|建议|推荐"|grep -v "<\|>"`
14   pkg_download=`apt-cache  depends $1|grep -E "依赖|建议|推荐"|grep -v "<\|>"|awk -F: '{print $2}'|tr '\n' '@'|sed "s/\@/:$2/g"`
15 }
16 
17 pkgDownload()
18 {
19 echo -e "\nBegin to download...\n"
20 if [ -d "./packages/" ];then
21         chmod 777 packages && cd packages && rm -rf * && apt-get download $dl_pkgs > /dev/null 
22 else
23         mkdir packages && chmod 777 packages && cd packages && apt-get download $dl_pkgs > /dev/null 
24 fi
25 ls -t ../packages | tr ' ' '\n'
26 echo -e "\n!!! Download Complete !!!\n"
27 }
28 
29 
30 lah=`sudo dpkg --print-architecture`
31 ah=$3
32 lv=$2
33 if [ ! $ah ];then
34         ah=$lah
35 fi
36 if [ ! $lv ];then
37         lv=0
38 fi
39 pkgs="$1:$ah"
40 i=0
41 echo -e "\nArchitecture:$ah"
42 
43 if [ $ah != $lah ];then
44     sudo dpkg --add-architecture $ah
45     echo -e "\nUpdate repo for architecture $ah...\n"
46     sudo apt-get update > /dev/null 
47 fi
48 
49 echo  -e "Search all of depends about $1 with lever $lv :\n"
50 while [ $i -lt $lv ];
51 do
52         let i++
53         all_list="$pkgs"
54         all_pkgs="$pkgs"
55         for p in $pkgs
56         do
57                 getDepends $p $ah
58                 all_list="$all_list $pkg_list"
59                 all_pkgs="$all_pkgs $pkg_download"
60         done
61         pkgs=$all_pkgs
62 done
63 
64 echo $all_list
65 dl_pkgs=`echo "$pkgs $all_pkgs"|tr ' ' '\n'|sort -ru|tr '\n' ' '`
66 echo $dl_pkgs
67 pkgDownload
68 
69 if [ "$ah" != "$lah" ];then
70         echo "Delete forgin-architecture:$ah"
71         sudo dpkg --remove-architecture $ah
72     sudo apt-get update >/dev/null
73 fi

 

posted on 2022-04-13 10:41  Cooper_73  阅读(2009)  评论(0编辑  收藏  举报

导航