I have a simple requirement. I want to define several variables that will correspond to any number of given packages I want to install via a shell script.
Sample code below:
MISC="shutter pidgin"
WEB="apache2 mongodb"
for pkg in $MISC $WEB; do
if [ "dpkg-query -W $pkg | awk {'print $1'} = """ ]; then
echo -e "$pkg is already installed"
else
apt-get -qq install $pkg
echo "Successfully installed $pkg"
fi
done
Everything kinda works, but the logic seems flawed because it's not reliably installing the packages I want. It either says they've been installed already or it's trying to install packages that have already been installed previously.
I've also been trying with command -v
or the following:
if [ "dpkg -l | awk {'print $2'} | grep --regexp=^$pkg$ != """ ]; then
And even with the -n
and -z
flags to check if the returned string was empty. Pretty sure I'm missing some good sense here.
Do you have any idea what I could do to make sure a package is actually installed or not?
Thanks!