shell script stop error shell script stop error shell shell

shell script stop error


Add the following to the top.

set -e

After executing that line, the shell will exit if any line returns an error code. set +e will turn this back off again (i.e. switch back to continuing regardless of any error return codes).

See http://www.davidpashley.com/articles/writing-robust-shell-scripts.html for further details.


You need to use set -e somewhere before executing this part.


You will have to query each step you have to check their return code. This means you will have to understand what the return codes for each query is (might be different for each). The example below checks vgextend for error code -1, then returns -1 itself.

pvcreate /dev/$1vgextend VolGroup00 /dev/$1if [ $? == -1 ]; then      echo "vgextend returned an error"      exit -1;filvextend --size +$2 /dev/VolGroup00/LogVol00resize2fs /dev/VolGroup00/LogVol00

Set -e might be overkill, as some errors may be tolerable under certain circumstances. In the example below, the rm will return an error if thefile does not exist, but it's ok to continue (Yes, I know we could a conditional delete, but the example is meant to illustrate the point being made.)

# delete the file, it it existsrm thefile# create the filetouch thefile