我目前使用以下命令添加官方 ubuntu 存储库:
sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe restricted multiverse"
但是这个命令添加 repo 时没有检查它们是否已经存在。所以我尝试使用 bash 循环,它负责在添加它们之前验证它们是否存在
为了做到这一点,我尝试使用这个循环:
add_ppa() {
for i in "$@"; do
grep -h "^deb.*$i" /etc/apt/sources.list.d/* > /dev/null 2>&1
if [ $? -ne 0 ]
then
echo "Adding ppa:$i"
sudo add-apt-repository -y ppa:$i
else
echo "ppa:$i already exists"
fi
done
}
也许它可以用于添加如下存储库:
add_ppa webupd8team/atom xorg-edgers/ppa ubuntu-wine/ppa
但不适用于:
add_ppa "deb http://archive.ubuntu.com/ubuntu focal main universe restricted multiverse"
Error: only one repository is needed as an argument
问题是 ubuntu 20.04,当我们手动选择“受限”和“多元宇宙”时,它会将这些词添加到存储库行中。
例子sources.list
:
前:
deb http://archive.ubuntu.com/ubuntu focal main
deb http://archive.ubuntu.com/ubuntu focal-updates main
deb http://archive.ubuntu.com/ubuntu focal-backports universe main
deb http://archive.ubuntu.com/ubuntu focal-security main
后:
deb http://archive.ubuntu.com/ubuntu focal main restricted multiverse
deb http://archive.ubuntu.com/ubuntu focal-updates main restricted multiverse
deb http://archive.ubuntu.com/ubuntu focal-backports universe main restricted multiverse
deb http://archive.ubuntu.com/ubuntu focal-security main restricted multiverse
我试过这个其他循环但它不起作用
the_ppa='deb http://archive.ubuntu.com/ubuntu focal main restricted universe multiverse'
if ! grep -q "^deb .*$the_ppa" /etc/apt/sources.list /etc/apt/sources.list.d/*; then
# commands to add the ppa ...
sudo add-apt-repository -y "$the_ppa"
fi
我该如何解决它?