-----------------------------------------------------------
#!/bin/bash
Set-e
Command 1
Command 2
...
Exit 0
----------------------------------------------------------
Every script you write should include SET-E in the top. This tells Bash is it should exit the script if any statement returns a non-true return value. The benefit of USING-E is that it prevents errors snowballing to serious issues when they could have been caught R. Again, for readability your may want to use Set-o errexit.
Every script you write should be prefixed with SET-E at the beginning of the file, which tells Bash that it should exit if the execution of any statement is not true. The advantage of this is that preventing errors from snowball-like causes a fatal error that should have been dealt with before. If you want to increase readability, you can use Set-o Errexit, which works the same as SET-E.
USING-E gives you the error checking for free. If you are forget to check something, bash'll do it for you. Unfortunately it means you can ' t check $? As bash would never get to the checking code if it isn ' t zero. There are other constructs your could use:
Use-e to help you check for errors. If you forget to check (execute the result of the statement), bash will help you execute it. Unfortunately, you will not be able to check the $?, because if the executed statement is not returned 0,bash will not be able to execute to the check code. You can use the other structure:
Command
if ["$?" -ne 0]; Then
echo "command failed";
Exit 1;
could be replaced with
can be replaced as
Or
Or
if! Command Then
echo "command failed";
Exit 1;
What If you have a command which returns Non-zero or you are does not interested in it return value? can use command | | True, or if you are have a longer section of code, you can turn off the error checking, but I recommend your use this sparingly .
If you have a command that returns a non 0 or you don't care about the results of the statement execution, you can use command | | True, or you have a long code that you can turn off error checking (without using set-e), but I suggest you use this statement conservatively.