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
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.
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.
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”.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With