Today's operation encountered a problem, need to redirect the content to the file, and then rename the use of the file, the result of the redirection has been thrown out of the exception, the following nature will be followed by a variety of problems. Therefore, it is still felt that some key operations to make some judgments or catch exceptions, to avoid the emergence of more serious problems.
The solution is as follows:
Using SET-E
The beginning of every script you write should contain set-e. This tells Bash to exit bash if any one of the statements returns a value that is not true. The advantage of using-e is to avoid making the error snowball into a serious error and catching the error as early as possible. More readable version: Set-o Errexit uses-E to free you from inspection errors. If you forget to check, bash will do it for you. But you have no way to use $? To get the command execution status because bash cannot get any return values that are not 0. You can use a different structure, using the command
Use command
If ["$?" -ne 0]; Then echo "Command failed"; Exit 1; Fi "can be replaced by: command | |
Shell statement error or abnormal auto Exit Do not continue execution