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.

I'm working on my bash script to create logical volumes. I have this command which I want to put in a if to check of each of these succeed. What is the easiest way to do that?

 #!/bin/bash
 pvcreate /dev/md5

vgcreate vg0 /dev/md5

lvcreate -L 200MB vg0 -n test
share|improve this question

5 Answers 5

up vote 5 down vote accepted
#!/bin/bash

pvcreate /dev/md5 && echo "pvcreate succeeded" || echo "pvcreate failed"

vgcreate vg0 /dev/md5 && echo "vgcreate succeeded" || echo "vgcreate failed"

lvcreate -L 200MB vg0 -n test && echo "lvcreate succeeded" || echo "lvcreate failed"

Logical AND && part will be executed only if the command succeeded(true) else Logical OR || (false) part will be executed.

share|improve this answer
    
not succeeded? That doesn't sound good for my ears. I'd go for failed. –  nyuszika7h Feb 17 at 18:16
#!/bin/ksh
safeRunCommand() {
typeset cmnd="$*"
typeset ret_code

echo cmnd=$cmnd
eval $cmnd
ret_code=$?
if [ $ret_code != 0 ]; then
  printf "Error : [%d] when executing command: '$cmnd'" $ret_code
  exit $ret_code
fi
}

command="ls -l | grep p"
safeRunCommand "$command"

Source: StackOverFlow

share|improve this answer

This would be traditional:

#!/bin/bash
pvcreate /dev/md5 || die "pvcreate failed"
vgcreate vg0 /dev/md5 || die "vgcreate failed"
lvcreate -L 200MB vg0 -n test || die "lvcreate failed"

The function die is something you'll have to define, which prints an error message and then exits. A sample implementation might be

die() {
     echo $@ 1>&2   # print arguments of 'die' to standard error
     exit 1         # exit the script
}

When you do it like this, the script won't produce any output unless something fails, in which case it will not go any further. If you want it to keep going even if a command fails, remove the exit line from the implementation of die.

If you want to get a message for each command that succeeds, as well, you could do this:

#!/bin/bash
if pvcreate /dev/md5; then
    echo "pvcreate succeeded"
else
    echo "pvcreate failed"
fi

and similarly for the other two commands.

share|improve this answer
#!/bin/bash
if pvcreate /dev/md5
then
  if vgcreate vg0 /dev/md5
  then
    lvcreate -L 200MB vg0 -n test
  fi
fi

Not testet. But should work.

Basic if-else structure is:

if condition
  then
    do somethin...
fi
share|improve this answer
3  
[ and ] are NOT part of the if syntax. [ is a shell builtin, an alias for test. The reason you end it with ] is to make it look symmetric. See mywiki.wooledge.org/BashFAQ/031 for more information. (Hint: You want simply if pvcreate /dev/md5 and so on.) –  nyuszika7h Feb 17 at 18:18
    
[pvcreate --- command not found. Please edit or remove this answer, it is plain wrong. –  Rmano Feb 17 at 21:12
    
@myuszika7h i have removed the [ ]. Was typed in a rush. –  rechengehirn Feb 17 at 21:36

If all the commands depend on the one before succeeding, you can use set -e:

#!/bin/bash
set -e

pvcreate /dev/md5
vgcreate vg0 /dev/md5
lvcreate -L 200MB vg0 -n test

Note that the above will exit without and specific message on which command failed.

share|improve this answer

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.