Install a list of packages only if they aren't already installed

Solution 1:

apt-get will fairly silently skip over any package that is installed already so I'm not sure why it needs to get special treatment? ie:

root@bun:~# apt-get -y install  vlc
Reading package lists... Done
Building dependency tree       
Reading state information... Done
vlc is already the newest version.

Is there a particular reason this won't work for you as-is?

Solution 2:

For a live session setup script, I had something like this:

# returns 1 if the package was already installed and 0 otherwise. The first
# argument is the package name to be checked (and installed if not already).
# other arguments are passed to apt-get
try_install() {
    dpkg -l "$1" | grep -q ^ii && return 1
    apt-get -y install "$@"
    return 0
}

if try_install openssh-server; then
    sed /etc/ssh/sshd_config 's/UsePAM yes/UsePAM no/' -i
    reload ssh
fi
try_install screen && wget lekensteyn.nl/files/screenrc -O ~/.screenrc
# passing extra options and package names to apt-get
try_install firefox --no-install-recommends firefox-kde-support

If an application was already installed, I assumed it to be configured.

Solution 3:

I went back over my asked questions on this site and realised I never posted the commands I ended up using:

export DEBIAN_FRONTEND=noninteractive # stop annoying prompts
dpkg -s "$@" > /dev/null 2>&1 || apt-get -qq -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install "$@"