How to only run the install command on non-existing packages?

I have a bash script, that I use for installing packages. This script contains lines like:

package="gcc"
echo "installing " $package
pamac install --no-confirm $package

I would like to only run the pamac install install command if the package is non-existing on the system.

Pseudo code of what I would like to achieve:

package="gcc"
if ! [ $package -eq INSTALLED ]
    echo "installing " $package
    pamac install --no-confirm $package
fi

Does anyone know how I can achieve this?

UPDATE
Since @megavolt gave the bash specific answer, that was selected as the solution. However, please do see the below answers from @guinux, as there is an alternative and simple way of solving this:

No option needed, by default pamac install command ignores already installed packages.
You can add multiple packages to install in one command

“–needed”

How? I can’t see there being any -needed option in pamac.

   INSTALL
       Install packages from repositories, path or url

       pamac install [options] <package(s),group(s)>

           --ignore <package(s)>
               ignore a package upgrade, multiple packages can be specified by separating them with a comma

           --overwrite <glob>
               overwrite conflicting files, multiple patterns can be specified by separating them with comma

           --download-only, -w
               download all packages but do not install/upgrade anything

           --dry-run, -d
               only print what would be done but do not run the transaction

           --as-deps
               mark all packages installed as a dependency

           --as-explicit
               mark all packages explicitly installed

           --upgrade
               check for updates

           --no-upgrade
               do not check for updates

           --no-confirm
               bypass any and all confirmation messages

UPDATE:
I see that there is one in pacman. I will check that tool instead. Thanks!

Hello @mzuniga :wink:

This is not a forum bash scripts in general. There are other sources… But i guess you are looking for something like that:

#!/bin/bash
installed=$(pacman -Qq) #OR $(pamac list --installed --quiet)
packages=("curl" "pamac" "new_packages")
list=()

for x in ${packages[@]}
        do
            if [[ $installed == *$x* ]]
                then
                    echo "Skipping1 $x"
            elif [[ $installed !=  *$x* ]]
                then
                    if [[ $(echo "$installed" | egrep -o "^$x$") != $x ]]
                        then
                    echo "Adding $x to list"
                    list+=("$x" )
                    else
                        echo "Skipping2 $x"
                    fi
            fi
done

if [[ -n "$list" ]]
    then
        sudo pacman -S --noconfirm  $list #OR pamac install --no-confirm $list
    else
        echo "Nothing to install"
fi

Hope it helps :wink:

2 Likes

No option needed, by default pamac install command ignores already installed packages.

Thanks a lot, @megavolt. This was just what I was looking for.

1 Like

Thanks, @guinux. However, running the install command for every package do take some time. Hence I want to avoid that.

You can add multiple packages to install in one command

1 Like

Ahh. That makes a massive difference. Thanks again, @guinux.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.