Monday, May 6, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 4961  / 2 Years ago, thu, may 12, 2022, 7:01:48

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


More From » apt

 Answers
6

I ended up writing a function to deal with ppa repositories.



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
}


I wonder if there is some more elegant way.


[#31260] Thursday, May 12, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kroapathe

Total Points: 445
Total Questions: 117
Total Answers: 99

Location: Botswana
Member since Sun, Sep 19, 2021
3 Years ago
;