Set and linuxset in linux
1. set-e
"Exit immediately if a simple command exits with a non-zero status ."
The code that appears after "set-e". Once the return value is non-zero, the entire script exits immediately.
2. set-o pipefail
"If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands in the pipeline exit successfully. this option is disabled by default."
After this setting is executed, the Code following it, including the return value of the pipeline command, is the return value of the last Non-zero command, or when all the commands in the pipeline are successfully executed, zero is returned.
Example:
When set-o pipifail is not set
#!/bin.bash# there is no a.test,but have b.testcat a.testecho $?cat b.testecho $?cat b.test | echo "hi"echo $?
cat a.test | echo "hi"
echo $?
The execution result is as follows:
linux-UMLhEm:/home/test/shell # sh -x tst.sh+ cat a.testcat: a.test: No such file or directory+ echo 11+ cat b.test----this is a test-----+ echo 00+ cat b.test+ echo hihi+ echo 00+ cat a.test+ echo hihicat: a.test: No such file or directory+ echo 00
We can see that when cat a. test | echo "hi" is executed, the result of the rightmost command execution is returned.
Set-o pipeline as follows:
set -o pipefailcat b.test | echo "hi"echo $?cat a.test | echo "hi"echo $?
The output result is as follows:
+ set -o pipefail+ cat b.test+ echo hihi+ echo 141141+ cat a.test+ echo hihicat: a.test: No such file or directory+ echo 11