Here the way i was able to check it:
## function to install missing packages - $1 : package name.
require_pkg() {
if ! command -v $1 >/dev/null; then
msg_warn missing_pkg $1
echo "Do you want to install $1 ? (works only with apt-get package manager) [y/N]"
read -r YESNO
if [[ $YESNO =~ ^([yY][eE][sS]|[yY])$ ]]; then
apt-get --force-yes --yes install $1
fi
echo "$1 will not be installed. This package is required so considere to install it ..."
fi
}
As you can see, i’ve used command -v
. But iirc there is like 3 or 4 methods to check if a package is installed! I’ve choosed this one because, iirc again, it was one of the most reliable in different linux OS.
For information, msg_warn is a function i’ve made in my script (not shown in this answer) to print a warning message “missing package”.
Hope this way to operate your question will inspire you :).