0

I use an installation script and here's two of my installation commands:

function InstallChrome()
{
    if ( which google-chrome 1>/dev/null ); then
        echo "Chrome is installed"
        return
    fi
echo "Installing Google Chrome ..."

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb -O chrome
sudo dpkg -i chrome

echo "Installed Google Chrome"

}

So basically I search for the program installed by apt and if it exists, I won't run the apt commands.

The reason is that it's way faster than letting the apt check.

However, this code does not work:

function InstallSshServer()
{
    if ( which openssh-server 1>/dev/null ); then
        echo "SSH Server is installed"
        return;
    fi
echo "Installing SSH Server ..."

sudo apt install openssh-server -y

echo "Installed SSH Server"

}

What is the name of the program openssh-server installs on my machine? How can I check if it's already installed or not?

  • Why do you need to test whether it's already installed or not? The apt-install command would not fail if it was installed. – Kusalananda Nov 15 '22 at 17:13
  • @Kusalananda, as I mentioned, it's faster. For an installation script that installs like 20 programs on a machine, the difference is clearly visibile. – Saeed Neamati Nov 16 '22 at 05:14
  • Even if you call apt install once with all the packages in the same invocation? You would not need to call apt install 20 times. – Kusalananda Nov 16 '22 at 09:21

3 Answers3

5

openssh-server installs /usr/sbin/sshd, you should look for that. Packages don’t necessarily install a binary by the same name, and the binaries they install aren’t necessarily on all users’ paths. Thus, explicitly:

[ -x /usr/bin/sshd ] || sudo apt install -y openssh-server

dpkg -L will tell you what files are installed by a given package, if that package is installed. Binaries can be listed with

dpkg -L openssh-server | grep bin/

apt-file list will show you files installed by a package without installing it first.

As an aside, Why not use "which"? What to use then? would make useful reading for your script.

Stephen Kitt
  • 434,908
1

What I would do using :

if ! type -p sshd &>/dev/null; then
    sudo apt-get install -y openssh-server
fi
0

Here is an adaptation of a little script I made for a similar case:

#!/bin/bash

prompt_confirm() { while true; do read -r -n 1 -p "${1:-} [y/n]: " REPLY case ${REPLY} in [yY]) echo ; return 0 ;; [nN]) echo ; return 1 ;; *) printf " \033[31m %s \n\033[0m" "Incorrect input, please type y (yes) ou n (no)." esac done }

Check if openssh-server is installed

if ! command -v sshd &> /dev/null then echo -e "\nThe openssh-server is not installed." if prompt_confirm "Would you like to install it now?"; then apt install -y openssh-server else echo -e "\nOk, quitting.\n" exit fi fi

Of course, you can use only the check part, the prompt y/n isn't required.

command -v is the recommended way to test if command is available on a system.

ramius
  • 853