#!/bin/bashset-ecommand 1command 2...exit 0
----------------------------------------------------------
every script you write Should include Set-e at the top. This tells bash, 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 has been caught Earlie R. Again, for readability-want to use Set-o errexit.
Each script you write should be prefixed with SET-E at the beginning of the file, which tells Bash to exit if the execution of any statement is not true. The upside is to prevent mistakes from snowball-like to cause 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 Error checking for free. If you forget to check something, bash would 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 is 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 do it for you. Unfortunately, you will not be able to check $? Because if the executed statement is not returned 0,bash will not be able to execute into the checked code. You can use other structures:
Copy
command IF ["$?" -ne 0]; Then echo "Command failed"; Exit 1; Fi
Could is replaced with
can be substituted for
Command | | {echo "command failed"; exit 1;}
Description:| | command not executed successfully, then execute {echo "command failed"; exit 1;}
Or
Or
Copy
if! Command Then echo "Command failed"; Exit 1; Fi
What if you had a command that returns Non-zero or is not a interested in its return value? can use command | | True, or if you had a longer section of code that you can turn off the error checking, but I recommend you use this sparingly .
If you have a command that returns non-0 or you do not care about the result of the statement execution, then you can use command | | True, or you have a very long code, you can turn off error checking (do not use set-e), but I still recommend that you use this statement conservatively.
Unix/linux the role of "set-e" in shell scripts