Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shell script stop error

Tags:

linux

shell

I have the following script and I want the script to stop executing on line x if line x encounters an error, how do I do that?

pvcreate /dev/$1
vgextend VolGroup00 /dev/$1
lvextend --size +$2 /dev/VolGroup00/LogVol00
resize2fs /dev/VolGroup00/LogVol00
like image 848
user678070 Avatar asked May 12 '11 10:05

user678070


People also ask

How do I stop a shell script from error?

To end a shell script and set its exit status, use the exit command. Give exit the exit status that your script should have. If it has no explicit status, it will exit with the status of the last command run.

Does bash script stop on error?

Bash Pitfalls Failed commands do not stop script execution In most scripting languages, if a function call fails, it may throw an exception and stop execution of the program. Bash commands do not have exceptions, but they do have exit codes.

How do I stop a bash script execution?

There are many methods to quit the bash script, i.e., quit while writing a bash script, while execution, or at run time. One of the many known methods to exit a bash script while writing is the simple shortcut key, i.e., “Ctrl+X”. While at run time, you can exit the code using “Ctrl+Z”.


1 Answers

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.

like image 51
Matt Sheppard Avatar answered Sep 23 '22 08:09

Matt Sheppard