1

Recently one of my bash scripts failed because cmake and zip / unzip were not installed on a system.

What would be a convenient way to check for installed packages in $PATH env?

I would like to check $PATH directly for cmake and the like since my script is running on Debian, Ubuntu, Arch, and so on. Thus, I would prefer to not use a package manager, because I would have to implement it several times using dpkg, pacman, ... based on the distribution the script is currently executed on.

daniel451
  • 1,067
  • 6
  • 16
  • 26
  • If you're just looking for tools like cmake and zip then those are installed by the package manager and the binaries will be in /usr/bin unless you built them from source and put them elsewhere. If you installed them via the package manager then you can just use the package tool like rpm or dpkg-query to search for the installed packages. – Nasir Riley Mar 09 '19 at 23:47
  • @NasirRiley yes, but the problem is that my script is running on different distributions. Hence, I would like to avoid implementing that with rpm, dpkg, pacman, ... It is quite cumbersome to maintain this by hand so I would prefer to not use different package managers. – daniel451 Mar 10 '19 at 00:11

1 Answers1

5

As mentioned in "Why not use "which"? What to use then?", the most portable way of testing whether a command is found in $PATH or not is through:

if command -v given-command > /dev/null 2>&1; then
  echo given-command is available
else
  echo given-command is not available
fi

If your script is for public consumption, you should obviously document the prerequisites so that another person is able to install the dependencies before running the code.

Kusalananda
  • 333,661