6

I have two repository that I want my script to verify if they are on the system. How can I verify by command line if a repository is already added to the system and, if not, add them?

3 Answers 3

10

You can see all repository enabled in terminal with this command below (It will print all repository line starting with deb or deb-src)

find /etc/apt/ -name *.list | xargs cat | grep  ^[[:space:]]*deb

Explanation: first find find all files with .list extension in /etc/apt directory and feeds xargs the list, which in turn print all text on all of those files to the display device i.e Monitor. Then grep select those lines of which starts with deb. I have used extra step to include those lines also which starts with whitespace characters instead of deb.

If you want to see only the repositories with binary packages not source packages (deb-src), use another grep to exclude entries with deb-src like this one:

find /etc/apt/ -name *.list | xargs cat | grep  ^[[:space:]]*deb | grep -v deb-src

To add the repository using command line, just use this kind of command:

(I am assuming your repository source line is like this deb http://mydomain.com/ubuntu precise main )

echo deb http://mydomain.com/ubuntu precise main | sudo tee -a /etc/apt/sources.list

This command uses tee program which is used to read and write from standard input to standard output. Here, it takes the echoed repo line and put that line at the end of /etc/apt/sources.list file which is the main repository source file.

Please note that without using -a option for tee the entire sources.list will be replaced with the just echoed line.

Alternatively you can use your own file for the custom repo without touching sources.list file. For example,

echo deb http://mydomain.com/ubuntu precise main | sudo tee -a /etc/apt/sources.list.d/teles.list

It will create a file in /etc/apt/sources.list.d directory with name teles.list containing line deb http://mydomain.com/ubuntu precise main.

Note that, to add repository source to the system your script need to be run with root priviledge

0

I would just use grep, if the repository name is in REPO variable, do something like this:

if ! grep -q "$REPO" /etc/apt/sources.list; then
  # do something if repo is not installed
fi
-1

I dont know if this is what you need but typing

software-properties-gtk

in a terminal or in dash-command windows will open a list of software sources.

Use this command to add your PPA

sudo add-apt-repository ppa:YOUR PPA HERE

Hope that helps

EDIT: You can edit the file with sudo nano /etc/apt/sources.list if that's needed. nano is a terminal text editor you may have to do this first

sudo apt-get install nano

to install it.

3
  • Isn't there a command line that make the same that SPG do? Because I don't have how to access the Gtk window to read the list of repos xD But thank you for your help! Aug 31, 2012 at 16:49
  • Plese see my edit to open the sources list in a terminal
    – Mark Kirby
    Aug 31, 2012 at 16:52
  • nano is installed by default in Ubuntu.
    – Anwar
    Aug 31, 2012 at 17:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .