1

I am working on a script to install a array raid5. I am having trouble with inserting auto=yes when the script ask: if I want to continue creating array. I tried --auto=yes (http://www.linuxmanpages.com/man8/mdadm.8.php) but very unsure where to place it.

#!/bin/bash
mdadm mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sdc1 /dev/sdd1 /dev/sde1 --spare-devices=1 /dev/sdf1

if [ $? -eq 0 ]; then
    echo OK
else
    echo FAIL
fi
user3185936
  • 1,567
  • 6
  • 23
  • 44

1 Answers1

5

1) you can use HEREDOC to resolve such problems.

for example for passwd command:

#!/bin/bash
passwd user <<EOF
mypassword
mypassword
EOF

You can also run your script and put heredoc :

./script <<EOF
>yes
>yes
>no
>EOF

UPDATE:

This is finally what you want

if you have one question:

./script <<EOF
>yes
>EOF

Also you can:

#!/bin/bash
mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sdc1 /dev/sdd1 /dev/sde1 --spare-devices=1 /dev/sdf1 <<EOF
yes
EOF

2) There is also another solution:

yes | command-that-asks-for-input

or, if a capital 'Y' is required:

yes Y | command-that-asks-for-input
MLSC
  • 5,872
  • 8
  • 55
  • 89
  • If i want to have it in a if as a script to check if the command ran or not. How will that work? – user3185936 Feb 17 '14 at 09:03
  • I didn't get you..what do you mean? – MLSC Feb 17 '14 at 09:57
  • This is how my script looks, but still not working. Getting errors. #!/bin/bash mdadm mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sdc1 /dev/sdd1 /dev/sde1 --spare-devices=1 /dev/sdf1 < – user3185936 Feb 17 '14 at 10:02
  • Here is the code: https://www.dropbox.com/s/5e5vjb9kbgjs4qw/kode.png Using virtualbox so mdadm code is not fully showed. Here is the error: https://www.dropbox.com/s/3la8kqhyob1h6fv/feilmelding.png – user3185936 Feb 17 '14 at 10:20
  • @user3185936 I assume you want to check the result of your action, yes? – MLSC Feb 17 '14 at 10:28
  • Yes I checked it by cat /proc/mdstat. It showed up as md127. But when i deleted the array and did the script again I got this error: https://www.dropbox.com/s/tykf1bx3j9vtgs3/kode2.png – user3185936 Feb 17 '14 at 10:34
  • Did and updated my script. Are you sure to have two mdadm's? With One it runs but asks me if i want to continue creating array. How to put in auto=yes? With two mdadm's I get errors. – user3185936 Feb 17 '14 at 10:48
  • I think with one mdadm should work...this solution or heredoc should work..else I dont know really :( – MLSC Feb 17 '14 at 10:59
  • Thank you for helping! With one mdadm It runs I only needed auto=yes. Please help a pakistani brother. – user3185936 Feb 17 '14 at 11:00
  • ok...it asks a question right? what answer do you want to put? – MLSC Feb 17 '14 at 11:01
  • just run `./script < – MLSC Feb 17 '14 at 11:02
  • find `This is finally what you want` in answer part – MLSC Feb 17 '14 at 11:04
  • That worked. But you have no idea how to put that in the script istead of the command line ? ./script <yes >EOF – user3185936 Feb 17 '14 at 11:06
  • Thank you brother! Very good script working now! Salam from Pakistan. – user3185936 Feb 17 '14 at 11:10
  • @user3185936 you can upvote and accept the answer to help other guys – MLSC Feb 17 '14 at 11:22
  • You found the answer and run away? :)] – MLSC Feb 17 '14 at 12:01
  • I have accepted answar but cant upvote it because of low reputation. – user3185936 Feb 17 '14 at 12:06
  • good..no problem/// in such ways guys that have same questions can understand this question is answered – MLSC Feb 17 '14 at 12:07