0

I need to Bash script which will install few packages. For Example: I need install packages: Molecule and Ansible

Bash script should do it and I need it for Ubuntu/Debian:

If Molecule exist > print "Molecule Installed"
else
apt-get install Molecule
then
if Ansible exist > print "Ansible installed"
else
apt-get install Ansible

Can you tell me how will look script in bash, which will contain the above instructions?

Thank you in advance!

BElluu
  • 183
  • 1
    You should definitely learn how to write such a script. Start here: http://linuxcommand.org/lc3_lts0010.php – mikewhatever Apr 13 '19 at 21:15
  • Maybe a half-duplicate of this (or one of it's duplicates): https://askubuntu.com/questions/879877/how-to-know-whether-a-particular-package-is-installed-on-ubuntu – Xen2050 Apr 14 '19 at 10:52

2 Answers2

3

You don't really need such script. Whenever, you attempt to install a package that is already installed, apt will output <pkg-name> is already the newest version. string. You can see an example of that here:

$ sudo apt-get install python3-numpy
[sudo] password for ubuntuadmin: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-numpy is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 323 not upgraded.
$

Thus your problem reduces to simply using apt command itself to install multiple packages

sudo apt-get install molecule ansible 
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
2

Open the terminal and type:

apt policy molecule ansible  

The output is self-explanatory. It will be similar to this example:

$ apt policy molecule ansible
ansible:
  Installed: 2.5.1+dfsg-1
  Candidate: 2.5.1+dfsg-1
  Version table:
 *** 2.5.1+dfsg-1 500
        500 http://archive.ubuntu.com/ubuntu/pub/ubuntu bionic/universe amd64 Packages
        500 http://archive.ubuntu.com/ubuntu/pub/ubuntu bionic/universe i386 Packages
        100 /var/lib/dpkg/status
N: Unable to locate package molecule
karel
  • 114,770