-1

I want to check whether a program like firefox exists on ubuntu or not. In case it is not installed, I want to install it. I studied this topic and got information about command -v p programName, but I didn't understand how can I check if the program is installed or not. I want to write this:

#If firefox not installed:
   sudo apt-get update
   sudo apt install firefox

but I don't know how to write the if condition part.

Pablo
  • 465
  • 1
  • 4
  • 14

1 Answers1

2
#!/usr/bin/env sh

if ! command -v firefox >/dev/null 2>&1
then
    sudo apt-get update
    sudo apt install firefox
fi

Also notice that not all Linux systems use apt-get and that if sudo is configured to request a password the script will stall and wait for user to type a password which might be confusing.

Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38
  • I'm supposed to use it on Ubuntu. So should I use an alternative for "apt-get"? But I think it is not possible to use these commands without sudo, yes? – Pablo Mar 29 '20 at 20:46
  • If you're going to use only on Ubuntu-based distros then it's ok. It's not about `sudo`, you need to be root. – Arkadiusz Drabczyk Mar 29 '20 at 20:52
  • Thanks. Is there any way to give the root access at the beginning of the code, so that each time the script will not stall and wait for user to type a password? or at least one time it asks for the password. – Pablo Mar 29 '20 at 21:03
  • 1
    You can run the entire script with sudo: `sudo ./script.sh` – Arkadiusz Drabczyk Mar 29 '20 at 21:08