Set and linuxset in linux

Source: Internet
Author: User

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

  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.