Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am working on a script to create md1 array, first to check if the array exist if not it will create it. I get command as command mdadm not found and a few others. The script wont run.

check="proc/mdstat | grep md1"

 if [ -e $check]; then
echo "MD1 is already here"
mdadm --create --verbose /dev/md1 --level=5 --raid-devices=3 /dev/sdc1 /dev/sdd1 /dev/sde1 --spare-devices=1 /dev/sdf1 <<EOF
if [ $? -eq 0 ]; then
    echo  "MD1 was created"
elif [$? -ne 0]; then
    echo "A fault has occured"
share|improve this question
up vote 3 down vote accepted

Your check variable is completely wrong. It's assigning the literal string "proc/mdstat | grep md1" to check.

To fix it, use this instead:

if grep -q -e 'md1:' -e 'md127:' /proc/mdstat; then
    echo "MD1 is already here"
    if echo yes | mdadm --create --verbose /dev/md1 --level=5 --raid-devices=3 /dev/sdc1 /dev/sdd1 /dev/sde1 --spare-devices=1 /dev/sdf1; then
        echo  "MD1 was created"
        echo "An error has occurred"

A few other notes:

  • Bash doesn't mandate braces for multi line statements, so it's often cleaner to omit them. You should always ensure proper indentation, though.
  • Bash's if statement doesn't require a [ .. ] construct. It can use any command; if the command exits with 0, it's considered true, else false.
  • The << EOF syntax is redundant, a simple echo yes | will do that.
share|improve this answer
Thank you! I get error: mdadm command not found. EDIT: need to install mdadm again - will tell you if it worked. – user3185936 Feb 18 '14 at 10:10
Thank you! Working pretty good – user3185936 Feb 18 '14 at 10:17
I have a question, sometimes it creates md as md127. Is it a way to check for both md1 and md127 then create it if none of these exist? – user3185936 Feb 18 '14 at 10:22
@user3185936: See my edit. It will search for either md1 or md127, and it will create md1 if none exist. – minerz029 Feb 18 '14 at 10:26
Thank you! I have another question. How can I check if these disk already is partioned? for d in sdc sdd sde sdf; do fdisk /dev/$d << EOF n p 1 t fd w EOF done – user3185936 Feb 18 '14 at 10:33

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.