Do someone know how to check if a PPA is already added to my system before i add it with the add-apt-repository command in a shell script.

That would be very helpful.

Thank you.

share|improve this question

You can do something like this:

the_ppa=...  # set appropriately

if ! grep -q "^deb .*$the_ppa" /etc/apt/sources.list /etc/apt/sources.list.d/*; then
    # commands to add the ppa ...
fi

Notes:

  • grep exits with success if it finds a match
  • The ! negates this, so the commands will be executed if there is no match = ppa not added
  • The -q flag makes grep quiet, so it doesn't print the matched lines. It would be just noise, we need only to know if there is a match or not, for which the exit code is perfect
  • The ^deb .* prefix is to make sure the matched line starts with deb, to exclude lines that are commented out
share|improve this answer
    
As it turns out, this will find ppas that have been commented out, but I want to find PPAs that have not been commented out, and @Ten-Coins answer does that. – Taytay Feb 17 '17 at 21:57
    
Excellent - thanks @janos! – Taytay Feb 20 '17 at 18:31

Open your terminal and type this command

grep ^ /etc/apt/sources.list /etc/apt/sources.list.d/* | grep fogger

replace fogger with PPA name.

enter image description here

share|improve this answer
1  
nice to see that frogger is still alive, I guess grep is not the right soluthion for an if statement within a shell script – Josh Nov 23 '13 at 15:41
    
you can search field with awk also. pretty faster. – Ten-Coin Nov 23 '13 at 17:56
    
I'm not familiar with awk but if you have a solution don't hesitate to post your ansawre and get your repetition. Ps. I like one liners pretty much – Josh Nov 24 '13 at 14:31
    
as I am on vacation can't post now with out testing properly. what's that downvote for ? – Ten-Coin Nov 24 '13 at 16:18
    
I'll test this with an if statement later that day. Sorry for the downvote. I sould not read such posts late at night. I gues this should work and will respond later if I have tested teh command. – Josh Nov 25 '13 at 17:19

Removing list.save files, sources, ensure only deb lines and count it for "fogger" ppa:

ppa_added=`grep ^ /etc/apt/sources.list /etc/apt/sources.list.d/* | grep -v list.save | grep -v deb-src | grep deb | grep fogger | wc -l`

echo $ppa_added
share|improve this answer
    
This also finds deb lines that are commented out, starting with # deb, and lines from files like foo-ppa.list.distUpgrade - is that intended? – Volker Siegel Dec 16 '14 at 3:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.