4

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
7
  • I'm quite the n00b with bash scripting, too. I find https://www.shellcheck.net/ extremely helpful.
    – MDeBusk
    Commented Sep 16, 2022 at 2:50
  • Are you sure dpkg is the program you want? Most of the parameters you're using aren't in the man page.
    – MDeBusk
    Commented Sep 16, 2022 at 2:58
  • 3
    Last comment for tonight: in case you're writing this script to do the job you want done and not as an exercise in bash scripting with arrays, you're making things hard on yourself. If you ask 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 did sudo apt install libreoffice firefox virtualbox vlc.
    – MDeBusk
    Commented Sep 16, 2022 at 3:04
  • Alright I will do that Commented Sep 16, 2022 at 3:07
  • I know I said the last comment was the last comment, but... you're asking dpkg the wrong question, I think. You're asking if a package is installed when you want to know if it isn't installed. Try dpkg -s packagename 2>&1 | grep "not installed" instead.
    – MDeBusk
    Commented Sep 16, 2022 at 3:23

2 Answers 2

4

Simply:

#!/bin/bash

pkgs=(libreoffice firefox virtualbox vlc)
sudo apt-get -y --ignore-missing install "${pkgs[@]}" 
  • the packages already installed will be ignored automatically
  • those that are not accessible will be ignored too with --ignore-missing
  • prefer apt-get than apt for scripting

From man apt:

apt provides a high-level commandline interface for the package management system. It is intended as an end user interface and enables some options better suited for interactive usage by default compared to more specialized APT tools like apt-get(8)


Another approach that I use personally, is to add every needed packages in a file line by line:

packages.list:

moreutils
util-linux
strace

Then I can call:

apt-get -y --ignore-missing install $(< packages.list)
1

I am also a noob to shell scripting, honestly i don't know about your approach but this is what I would do instead.

#!/usr/bin/env bash

declare -a PKGS=(libreoffice firefox virtualbox vlc)
for i in "${PKGS[@]}"; do
    check=$(command -v "$i")
    if [[ $? -ne 0 ]]; then
        sudo apt install "$i"
    fi
done
2
  • Not all packages are direct commands, one example: command libreoffice-help-fr. Testing if package is installed is useless Commented Dec 28, 2022 at 16:28
  • And if you need to test if a package is installed: if dpkg -l | grep -wq <PACKAGE>; then echo installed; else echo not installed; fi No need to test explicitely $? Commented Dec 28, 2022 at 16:31

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.