Ubuntu – shell script to conditionally add apt repository

aptrepositoryscripts

I want to write a shell script that will add an apt repository.
I know that I can do it using sudo add-apt-repository -y <repo>.

My question is can I do it only if the repository was not added already, something like:

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

Thanks

Best Answer

I changed Itay's function so that it handles multiple parameters:

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
}

To be called like this:

add_ppa webupd8team/atom xorg-edgers/ppa ubuntu-wine/ppa
Related Question