1

I want to check from script if a command exists and suggest installation of the corresponding package if necessary.

On Debian systems I use dpgk to look for packages providing some specific file, e.g.:

$ dpkg -S */envsubst
gettext-base: /usr/bin/envsubst

Is there some solution that works on all Linux systems, or at least the most common ones?

Some open source library is welcome as well if there is not a few-liner.

3
  • Ubuntu does this. Type in a command, and, if it's not found, automatically recommends which package(s) to possibly install. Unfortunately, my Google-fu can't find how it works. Commented Apr 4, 2023 at 6:00
  • 1
    @RonJohn cnf aka. command-not-found Commented Apr 4, 2023 at 8:47
  • @NicolasFormichella, does that work for non-binaries (anything that shouldn't be in $PATH, anyway)? Commented Jul 5 at 12:22

2 Answers 2

3

On distributions supporting PackageKit, you can use that:

$ pkcon search file envsubst
Searching by file             [=========================]
Starting                      [=========================]
Loading cache                 [=========================]
Querying                      [=========================]
Finished                      [=========================]
Installed       gettext-base-0.21-4.amd64 (installed:debian-stable-main)        GNU Internationalization utilities for the base system
Installed       git-1:2.30.2-1+deb11u2.amd64 (installed:debian-stable-security-main)   fast, scalable, distributed revision control system

Unfortunately for more reliable results across distributions you need to specify the full path, which means in your case you’d have to try various possible installation locations. Thus on Debian:

$ pkcon search file /usr/bin/envsubst
Searching by file             [=========================]         
Loading cache                 [=========================]         
Querying                      [=========================]         
Finished                      [=========================]         
Installed       gettext-base-0.21-4.amd64 (installed:debian-stable-main)        GNU Internationalization utilities for the base system

and on RHEL 8:

$ pkcon search file /usr/bin/envsubst
Searching by file             [=========================]         
Finished                      [=========================]         
Installed       gettext-0.19.8.1-17.el8.x86_64 (installed:anaconda)             GNU libraries and utilities for producing multi-lingual messages
Available       gettext-0.19.8.1-14.el8.x86_64 (rhel-8-for-x86_64-baseos-rpms)  GNU libraries and utilities for producing multi-lingual messages

Many distributions also install command-not-found which will tell you how to make a given command available, if possible, or suggest typo candidates.

5
  • Oh! I feel like a newb, never new about pkcon. At least on my fedora 37, you do have to specify the full path of the file you're interested in: pkcon search file envsubst yields no results; pkcon search file /usr/bin/envsubst does tell me about the gettext-envsubst package. Sadly, pkcon search seems to reject globs and regexes. Commented Apr 4, 2023 at 8:15
  • Yes, pkcon search is rather limited — it’s a thin shim over the underlying packaging system. I hadn’t tested on RHEL or Fedora, thanks for the heads-up! Commented Apr 4, 2023 at 8:20
  • mehh, I went to http//www.packagekit.org as listed in the pkcon man page in hopes to figure out whether prepending */ to the search pattern if on a dnf repoquery distro would be an easy solution to these behavioral differences. That domain got occupied by a third party :( Commented Apr 4, 2023 at 8:26
  • Thanks so far! There does not seem to be a simple solution. With pkcon (hopefully that it actually is installed on the target distribution) you need to know the cross distri paths. Without, you might get improper results. Further more also the "machine readable" output has to be parsed. Well, it could be a workaround to give the user some hint where to find the requirements. However, I'ld like some precise end-user instruction similar to the command-not-found output. Commented Apr 9, 2023 at 2:06
  • To dynamically acquire the full path for something inside $PATH, utilise pkcon search file $(command -v envsubst). Commented Jul 5 at 12:23
0

You can use the pkgs.org web service online, as it supports most distros you'd ever want, not affiliated but very useful

If you want it from the CLI, you can subscribe to the API for $5 a month and build a small bash function to connect to it

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.