Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[bash] Finding installed packages

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
MCaspers
Posts: 4
Joined: 2023-02-01 13:21

[bash] Finding installed packages

#1 Post by MCaspers »

Hi,

Can someone help with this?

Given Debian 11.6:

I'm writing a bash script with a given list of package names that checks if they are installed on the system, and if installed get the version number of the installed package.
dpkg-query and dpkg -s help with that, and for most things this works perfectly well.

However for several packages I have to use more generic names like libc-dev or python.
dpkg-query and dpkg-s fail for this purpose however.

I can see that they are installed using apt install:
apt install libc-dev
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Note, selecting 'libc6-dev' instead of 'libc-dev'
libc6-dev is already the newest version (2.31-13+deb11u5).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded

So apt knows that libc-dev is installed, as libc6-dev.

I know a version of Python is basically always installed on Debian, for example in Debian 11 it's 3.9.2, but dpkg -s python says it's not installed.
And the apt install trick isn't working for this one either, because it'd try to install python 2 for a weird reason:

apt install python
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Note, selecting 'python-is-python2' instead of 'python'
The following additional packages will be installed:
libpython2-stdlib libpython2.7-minimal libpython2.7-stdlib python2 python2-minimal python2.7 python2.7-minimal
Suggested packages:
python2-doc python-tk python2.7-doc binfmt-support
The following NEW packages will be installed:
libpython2-stdlib libpython2.7-minimal libpython2.7-stdlib python-is-python2 python2 python2-minimal python2.7 python2.7-minimal
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 3,977 kB of archives.
After this operation, 16.2 MB of additional disk space will be used.
Do you want to continue? [Y/n] n
Abort.

Which of course I don't want.
I don't want anything to be installed at all, I just want to know if it's installed and if so which version.

So the question here is if there is a generic way to detect if a package is installed by its generic name rather than by its specific (major) version? If so, how?

Just so you know, the list of packages is provided on the command line or via a text file so there's no way of knowing up front which packages will be checked.

User avatar
fabien
Forum Helper
Forum Helper
Posts: 620
Joined: 2019-12-03 12:51
Location: Anarres (Toulouse, France actually)
Has thanked: 60 times
Been thanked: 146 times

Re: [bash] Finding installed packages

#2 Post by fabien »

I'm not sure what exactly you want to achieve. There seem to be two types of answers to your question.

Packages like libc-dev or python are virtual packages. This means that none, one, or several packages can fulfill the functionality that they represent. If no package fulfills this functionality, this virtual package is called an unmet dependency in man 8 apt-cache. The libc-dev and python virtual packages are fulfilled by only one provider each (libc6-dev and python-is-python2) on my system at this time.
So the first answer is that since virtual packages are never installed as such on the system, it would be correct to be told that they are not installed.

Now it is clear that you're interested in the real packages that provide these virtual packages functionalities. So your script should, for each package, a)test whether it is virtual, b)if it is virtual, list all real packages that provide it, c)check if any (one or several, for instance x-terminal-emulator is provided by gnome-terminal, konsole, lxterminal, xfce4-terminal, xterm, and others) of these packages are installed.

Note that it is not for a weird reason that apt installs python2 when asked to install python. python is, as seen above, provided by python-is-python2 which depends on python2 which in turn depends on python2-minimal and python2.7 and so on. But note that python2 could be installed without python-is-python2 installed, and in that case your script would say nothing when asked about python.

HTH

User avatar
sunrat
Administrator
Administrator
Posts: 6412
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 116 times
Been thanked: 461 times

Re: [bash] Finding installed packages

#3 Post by sunrat »

The current virtual package for python is called python3 btw. There is no actual package called python in Bullseye or later.
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!

Post Reply