Functions of "set-e" in Unix/Linux scripts

Source: Internet
Author: User
Functions of set-e in Unix/Linux scripts -----------------------------------------------------------#! /Bin/bashset-ecommand1command2... exit0 --------------------------------------- Unix/Linux script in the "set-e" role -----------------------------------------------------------#! /Bin/bashset-ecommand 1 command 2... exit 0 ---------------------------------------------------------- Every script you write shocould include set-e at the top. this tells bash that it shoshould exit the script if any statement returns a non-true return value. the benefit of using-e is that it prevents errors snowballing into serious issues when they cocould have been caught earlier. again, for readability You may want to use set-o errexit. each script you write should contain set-e at the beginning of the file. this statement tells bash to exit if the execution result of any statement is not true. The advantage of this is to prevent a fatal error from becoming larger like a snowball, which should have been handled before. To increase readability, you can use set-o errexit, which serves the same purpose as set-e. Using-e gives you error checking for free. If you forget to check something, bash will do it for you. Unfortunately it means you can't check $? As bash will never get to the checking code if it isn't zero. There are other constructs you cocould use: use-e to help you check errors. If you forget to check (the result of executing the statement), bash will help you execute it. Unfortunately, you will not be able to check $ ?, If the executed statement does not return 0, bash cannot execute the check code. You can use another structure: [plain] command if ["$? "-Ne 0]; then echo" command failed "; exit 1; fi cocould be replaced with can be replaced with [plain] command | {echo" command failed "; exit 1;} or [plain] if! Command; then echo "command failed"; exit 1; fi What if you have a command that returns non-zero or you are not interested in its return value? You can use command | true, or if you have a longer section of code, you can turn off the error checking, but I recommend you use this sparingly. if you have a command that returns non-0 values or you don't care about the result of statement execution, you can use command | true, or you have a long piece of code, you can disable the error check (no set-e is used), but I suggest you use this statement conservatively.

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.