shell脚本有条件地添加apt存储库

我想写一个shell脚本,它将添加一个apt存储库。
我知道我可以使用sudo add-apt-repository -y 来做到这一点。

我的问题是,只有在尚未添加存储库的情况下才能执行此操作,例如:

 if repo was not added yet: sudo add-apt-repository -y  sudo apt-get update 

谢谢

我更改了Itay的function,以便它处理多个参数:

 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 

我最终编写了一个处理ppa存储库的函数。

 add_ppa() { grep -h "^deb.*$1" /etc/apt/sources.list.d/* > /dev/null 2>&1 if [ $? -ne 0 ] then echo "Adding ppa:$1" sudo add-apt-repository -y ppa:$1 return 0 fi echo "ppa:$1 already exists" return 1 } 

我想知道是否有更优雅的方式。

现在可以在添加之前删除存储库:

 sudo add-apt-repository -r $REPO sudo add-apt-repository $REPO