I'm quite a newbie when it comes to bash. I'm trying to create a script that checks whether a package is installed or not. If not it will install that package. Not sure what I'm doing tbh.
#! /bin/bash
echo Installing/Checking packages from list
declare -a PKGS=("libreoffice", "firefox", "virtualbox", "vlc")
PKG_OK=$(dpkg-i -W --showformat='${Status}\n' PKGS |grep "install ok installed")
for i in "${PKGS[@]}"
do
if [ "" = "$PKG_OK" ]; then
echo "No $PKGS, installing the package now"
sudo apt install $PKGS[@]
fi
done
dpkg
is the program you want? Most of the parameters you're using aren't in the man page.apt
to install a package that's already installed, it just tells you it's already installed and tries the next one. It'd be fine if you just didsudo apt install libreoffice firefox virtualbox vlc
.dpkg
the wrong question, I think. You're asking if a package is installed when you want to know if it isn't installed. Trydpkg -s packagename 2>&1 | grep "not installed"
instead.