@FlorianDiesch'answer looks interesting, but I want something without need for any software not installed by default.
I ended up using this:
function check_packages_and_install_if_absent()
{
for PACKAGENAME
do
if
dpkg -l "$PACKAGENAME" | grep ^ii
then
echo "Package $PACKAGENAME is present"
continue
fi
FOUND=""
while read ACTUALPACKAGENAME
do
echo "$PACKAGENAME is a virtual package, that can be provided by $ACTUALPACKAGENAME"
if
dpkg -l "$ACTUALPACKAGENAME" | grep ^ii
then
echo "Actual package $ACTUALPACKAGENAME is present"
FOUND=true
break;
fi
done < <( apt-cache showpkg "${PACKAGENAME}" | sed -e '1,/^Reverse Provides: *$/ d' -e 's/ .*$//' | sort | uniq )
# Using sed to print lines after match
# https://stackoverflow.com/questions/32569032/sed-print-all-lines-after-match#answer-32569573
if [[ "$FOUND" == "true" ]]
then
continue
fi
echo "Package $PACKAGENAME is absent, installing"
sudo apt-get install -y "$PACKAGENAME"
done
}
You can call it with a list of normal and/or virtual package names:
function check_packages_and_install_if_absent foo bar baz
Reverse Provides
in the output. – darkdragon Jun 23 '20 at 06:01apt-cache show
(and install). – apaderno Nov 08 '20 at 21:42