'How to make a script to check if a package is installed and install it if not?

I am trying to make a bash script for work that would check the system for a handful of installed packages and if it dosent find them, install them. I have attached what I have done so far below and would appreciate any help.

#!/bin/sh

systemctl stop unattended-upgrades

apt purge unattended-upgrades

apt install curl nano lvm2 rsync net-tools

apt update

apt upgrade

echo "script is done!"



Solution 1:[1]

Thoughts

Like what others have said, you can just tell your script to do the install and nothing will happen if it fails. There are numerous ways to check if the package is already installed, I've providing a simple loop check that puths the results in an array. With that you could call the installer for those packages only if the array size is > 0.

Example

#!/bin/bash
declare -a isarray=('xxx' 'curl' 'nano' 'lvm2' 'rsync' 'net-tools')
declare -a installed=()
declare -a not_installed=()

for i in "${isarray[@]}" ; do
    if apt list | grep -q "^${i}/" ;then 
        installed+=(${i})
    else
        not_installed+=(${i})
    fi 
done 
echo ${not_installed[@]}

Explaination

  • isarray is an array of strings to look for on Ubunutu using apt list
  • installed / not_installed are results

if in the list of all packages installed contains xxx add to installed or not_installed. Continue to check all of strings in isarray and generate two results arrays.

Solution 2:[2]

systemctl stop unattended-upgrades
apt purge unattended-upgrades
apt-get -y update
apt-get -y upgrade

REQUIRED_PKG="nano"
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $REQUIRED_PKG|grep "install ok installed")
echo Checking for $REQUIRED_PKG: $PKG_OK
if [ "" = "$PKG_OK" ]; then
  echo "No $REQUIRED_PKG. Setting up $REQUIRED_PKG."
  sudo apt-get --yes install $REQUIRED_PKG
fi

REQUIRED_PKG="curl"
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $REQUIRED_PKG|grep "install ok installed")
echo Checking for $REQUIRED_PKG: $PKG_OK
if [ "" = "$PKG_OK" ]; then
  echo "No $REQUIRED_PKG. Setting up $REQUIRED_PKG."
  sudo apt-get --yes install $REQUIRED_PKG
fi

REQUIRED_PKG="lvm2"
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $REQUIRED_PKG|grep "install ok installed")
echo Checking for $REQUIRED_PKG: $PKG_OK
if [ "" = "$PKG_OK" ]; then
  echo "No $REQUIRED_PKG. Setting up $REQUIRED_PKG."
  sudo apt-get --yes install $REQUIRED_PKG
fi

REQUIRED_PKG="rsync"
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $REQUIRED_PKG|grep "install ok installed")
echo Checking for $REQUIRED_PKG: $PKG_OK
if [ "" = "$PKG_OK" ]; then
  echo "No $REQUIRED_PKG. Setting up $REQUIRED_PKG."
  sudo apt-get --yes install $REQUIRED_PKG
fi
REQUIRED_PKG="rsync"
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $REQUIRED_PKG|grep "install ok installed")
echo Checking for $REQUIRED_PKG: $PKG_OK
if [ "" = "$PKG_OK" ]; then
  echo "No $REQUIRED_PKG. Setting up $REQUIRED_PKG."
  sudo apt-get --yes install $REQUIRED_PKG
fi
REQUIRED_PKG="net-tools"
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $REQUIRED_PKG|grep "install ok installed")
echo Checking for $REQUIRED_PKG: $PKG_OK
if [ "" = "$PKG_OK" ]; then
  echo "No $REQUIRED_PKG. Setting up $REQUIRED_PKG."
  sudo apt-get --yes install $REQUIRED_PKG
fi

echo "Script is complete"

this is what I ended up with after getting some help from colleagues. this script works exactly how I intended, being able to search for installed snap packages and will install them if they are not found.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Colby Allison