The role of "SET-E" in the "Go" unix/linux script

Source: Internet
Author: User

Original link: http://blog.csdn.net/todd911/article/details/9954961

-----------------------------------------------------------
#!/bin/bash

Set-e

Command 1
Command 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:

[Plain]View Plaincopy
    1. Command
    2. If ["$?" -ne 0]; Then
    3. echo "Command failed";
    4. Exit 1;
    5. Fi

Could is replaced with

can be substituted for

[Plain]View Plaincopy
    1. Command | | {echo "command failed"; exit 1;}

Or

Or

[Plain]View Plaincopy
    1. if! Command Then
    2. echo "Command failed";
    3. Exit 1;
    4. Fi

What if you had a command that returns Non-zero or is not the 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 don't care about the result of the statement execution, 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 suggest you use this statement conservatively.

The role of "SET-E" in the "Go" unix/linux script

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.