以确定性方式启用“源”下载的脚本

以确定性方式启用“源”下载的脚本

我编写了几个脚本来帮助用户提取他们当前的内核源码包,应用我的一些补丁,并将其构建为一个.debmake-kpkg。需要手动干预的几个步骤之一是通过以下方式启用“源”下载:

  • “Ubuntu 软件中心”
    • 编辑...
      • 软件源...
        • “源代码”

在此处输入图片描述

有没有办法通过命令行自动执行此操作?据我所知,我可以取消注释前几行“deb-src” /etc/apt/sources.list,如下所示:

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://ca.archive.ubuntu.com/ubuntu/ trusty main restricted
deb-src http://ca.archive.ubuntu.com/ubuntu/ trusty main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://ca.archive.ubuntu.com/ubuntu/ trusty-updates main restricted
deb-src http://ca.archive.ubuntu.com/ubuntu/ trusty-updates main restricted

但这有点容易出错,因为sed如果源不符合默认顺序或其他各种原因,我编写的脚本不一定会起作用。更重要的是,我希望它能在 Ubuntu 14.04、Ubuntu 16.04 等上运行(即尽可能通用)。

是否有任何与命令行apt相关的工具可以通过 shell 脚本完成此操作?

谢谢。

答案1

最后,我只是启用了所有的srcrepos。

sed -i '/^#\sdeb-src /s/^#//' "/etc/apt/sources.list"

这不需要将操作系统升级到任意版本。

答案2

我遇到了和你类似的问题,最后我开始执行以下脚本,编写一个临时脚本sources.list,然后再次将其删除:

# making a copy of the users original sources.list file
cp -v /etc/apt/sources.list /etc/apt/sources.list.orig &&

# writing a custom sources.list file
cat > /etc/apt/sources.list << "EOF"
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted
deb http://archive.ubuntu.com/ubuntu/ xenial main restricted
deb-src http://archive.ubuntu.com/ubuntu/ xenial multiverse main universe restricted
deb http://archive.ubuntu.com/ubuntu/ xenial-updates main restricted
deb-src http://archive.ubuntu.com/ubuntu/ xenial-updates multiverse main universe restricted
deb http://archive.ubuntu.com/ubuntu/ xenial universe
deb http://archive.ubuntu.com/ubuntu/ xenial-updates universe
deb http://archive.ubuntu.com/ubuntu/ xenial multiverse
deb http://archive.ubuntu.com/ubuntu/ xenial-updates multiverse
deb http://archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://dearchive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://security.ubuntu.com/ubuntu xenial-security main restricted
deb-src http://security.ubuntu.com/ubuntu xenial-security multiverse main universe restricted
deb http://security.ubuntu.com/ubuntu xenial-security universe
deb http://security.ubuntu.com/ubuntu xenial-security multiverse
EOF

# running an update
apt-get update &&
apt-get -y dist-upgrade &&

# DO YOUR STUFF

# putting back the original sources.list
mv -v /etc/apt/sources.list /etc/apt/sources.list.tmp &&
cp -v /etc/apt/sources.list.orig /etc/apt/sources.list &&

# removing backup files
rm -rfv /etc/apt/sources.list.tmp &&
rm -rfv /etc/apt/sources.list.orig

答案3

我编写了一个 perl 单行程序,它只会取消注释deb-src位于未注释deb行之前的行:

perl -pi.orig -0e 's/^(deb .*\n)# (deb-src)/$1$2/mg' /etc/apt/sources.list

它还将保留原始版本/etc/apt/sources.list.orig。(因此您可以稍后根据需要恢复它)

应该跨版本工作,假设他们继续保持将注释掉的deb-src行放在相应deb行之后的习惯。

答案4

将各个部分连接在一起,我想到了一个可以轻松启用和禁用源下载的可逆解决方案。

启用源

云和 Stéphane Gourichon 的解决方案, 和sed 已修改,忽略不匹配的行和输出使用 sudo 重定向到临时文件下sources.list.d/

sed -e '/^#\sdeb-src /s/^# *//;t;d' "/etc/apt/sources.list" \
| sudo tee /etc/apt/sources.list.d/source-repos-tmp.list > /dev/null

禁用源

sudo rm /etc/apt/sources.list.d/source-repos-tmp.list

相关内容