Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Working on a bash script which first will check if mdadm is installed, if not it will install it. This is my script but it's not working correctly. Hope someone can help me.

 if [ "$mdadm" = "mdadm" ]
    then
  echo -e "Mdadm is installed.\n"
   else
   echo -e "Install mdadm .\n"
   aptitude -y install $mdadm

   fi
share|improve this question
    
You don't need to check whether it's installed. –  Ramchandra Apte Feb 15 at 17:13

1 Answer 1

up vote 2 down vote accepted

This is the needed script (say script.sh)

#!/bin/sh

for P; do
    dpkg -s "$P" >/dev/null 2>&1 && {
        echo "$P is installed."
    } || {
        echo "Installing $P"
        aptitude -y install $P
    }
done

Usage: script.sh mdadm

You can also check for as many packages as you need

Usage: script.sh mdadm package2 package3 ...

EDIT: For only one package you can do the following:

    dpkg -s "mdadm" >/dev/null 2>&1 && {
        echo "mdadm is installed."
    } || {
        echo "Installing mdadm"
        aptitude -y install mdadm
    }
share|improve this answer
    
Great, thanks! If you want to specify the package in the script is it to change the p to package name ? –  user3185936 Feb 15 at 10:55
    
yes my dear. Simply remove the for loop and replace $P with <yourpackagename>.plz don't forget to upvote the answer. –  kamil Feb 15 at 10:55
    
If you need it I will edit the answer –  kamil Feb 15 at 10:56
    
That would be awesome if you could edit the answer. I will surely try to do it my my self, but very unsure. –  user3185936 Feb 15 at 10:59
    
@user3185936 check the edit –  kamil Feb 15 at 11:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.