Set-e and Set-o in Linux Pipefail

Source: Internet
Author: User

1, SET-E

"Exit immediately if a simple command exits with a Non-zero status."

In the code that appears after "Set-e", once the return value is nonzero, 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 Z  Ero if all commands in the pipeline exit successfully. This option was disabled by default. "

After this setting is executed, its subsequent code, including the return value of the pipe command, returns a value for the last nonzero command, or returns zero when all the commands inside the pipeline are executed successfully.

As shown in the following example:

When the Set-o Pipifail is not set

#!/bin.bash# There is no a.test,but has b.testcat a.testecho $?cat b.testecho $?cat b.test | echo "HI" echo $?

Cat A.test | echo "HI"
echo $?

The results of the implementation are 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----thi S is a test-----+ echo 00+ cat b.test+ echo hihi+ echo 00+ cat a.test+ echo hihicat:a.test:no such file or directory+ EC Ho 00

You can see the cat a.test in execution | echo "HI" returns the result of the rightmost command execution.

Here's an example of setting up Set-o pipeline, as follows:

Set-o Pipefailcat B.test | echo "HI" Echo $?cat a.test | echo "HI" echo $?

The output results are 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 1 1

  

Set-e and Set-o in Linux Pipefail

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.