Wednesday, May 8, 2024
9
rated 0 times [  9] [ 0]  / answers: 1 / hits: 8199  / 1 Year ago, sun, may 7, 2023, 11:08:02

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?


More From » command-line

 Answers
2

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


[#35824] Tuesday, May 9, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ellter

Total Points: 311
Total Questions: 111
Total Answers: 117

Location: Lithuania
Member since Thu, Jul 14, 2022
2 Years ago
;