I\'m working on a Ubuntu system and currently this is what I\'m doing:
if ! which command > /dev/null; then
echo -e \"Command not found! Install? (y/n)
dpkg -s programmatic usage with automatic install
I like dpkg -s as it exits with status 1 if any of the packages is not installed, making it easy to automate:
pkgs='qemu-user pandoc'
if ! dpkg -s $pkgs >/dev/null 2>&1; then
sudo apt-get install $pkgs
fi
man dpkg does not document the exit status unfortunately, but I think it should be reasonably safe to rely on it:
-s, --status package-name...
Report status of specified package.
One thing to note is that running:
sudo apt remove
does not necessarily remove all files immediately for some packages (but does for others, not sure why?), and just marks the package for removal.
In this state, the package appears to be still usable, and as its files are still present, but it is marked for removal later on.
For example if you run:
pkg=certbot
sudo apt install -y "$pkg"
dpkg -s "$pkg"
echo $?
sudo apt remove -y "$pkg"
dpkg -s "$pkg"
echo $?
ls -l /usr/lib/python3/dist-packages/certbot/reporter.py
sudo apt remove --purge certbot
dpkg -s "$pkg"
echo $?
ls -l /usr/lib/python3/dist-packages/certbot/reporter.py
then:
the first two echo $? output 0, only the third one outputs 1
the output for the first dpkg -s certbot contains:
Status: deinstall ok installed
while the second says:
Status: deinstall ok config-files
and it only disappears after purge:
dpkg-query: package 'certbot' is not installed and no information is available
the file /etc/logrotate.d/certbot is still present in the system after apt remove, but not after --purge.
However, the file /usr/lib/python3/dist-packages/certbot/reporter.py is still present even after --purge.
I don't understand why, but with the hello package the second dpkg after apt remove shows that he package has already been removed without --purge:
dpkg-query: package 'hello' is not installed and no information is available
The documentations are also very unclear, e.g.:
sudo apt dselect-upgrade
did not remove certbot when it was marked as deinstall, even though man apt-get seems to indicate that:
dselect-upgradeis used in conjunction with the traditional Debian packaging front-end, dselect(1). dselect-upgrade follows the changes made by dselect(1) to the Status field of available packages, and performs the actions necessary to realize that state (for instance, the removal of old and the installation of new packages).
See also:
Tested on Ubuntu 19.10.
Python apt package
There is a pre-installed Python 3 package called apt in Ubuntu 18.04 which exposes an Python apt interface!
A script that checks if a package is installed and installs it if not can be seen at: How to install a package using the python-apt API
Here is a copy for reference:
#!/usr/bin/env python
# aptinstall.py
import apt
import sys
pkg_name = "libjs-yui-doc"
cache = apt.cache.Cache()
cache.update()
cache.open()
pkg = cache[pkg_name]
if pkg.is_installed:
print "{pkg_name} already installed".format(pkg_name=pkg_name)
else:
pkg.mark_install()
try:
cache.commit()
except Exception, arg:
print >> sys.stderr, "Sorry, package installation failed [{err}]".format(err=str(arg))
Check if an executable is in PATH instead
See: How can I check if a program exists from a Bash script?