Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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" ]
  echo -e "Mdadm is installed.\n"
   echo -e "Install mdadm .\n"
   aptitude -y install $mdadm

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

1 Answer 1

up vote 2 down vote accepted

This is the needed script (say


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

Usage: mdadm

You can also check for as many packages as you need

Usage: 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 '14 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 '14 at 10:55
If you need it I will edit the answer –  kamil Feb 15 '14 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 '14 at 10:59
@user3185936 check the edit –  kamil Feb 15 '14 at 11:00

Your Answer


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.