Shell Script Set+e Set-e

Source: Internet
Author: User

SET-E # Exit The script if an error happens

Script A

Set +e # don ' t bail out of bash script if CCache doesn ' t exist

Script B

SET-E # Back to regular ' bail out on error ' mode

Script C

As a script shell above,

The script in a and C executes when the return value is nonzero, and the entire script exits immediately.

If a return value of non 0 appears in the script B, the following script C will continue to execute.

Set-e

The-e parameter of the SET command, Linux comes with the following description:
"Exit immediately if a simple command exits with a Non-zero status."
In other words, the code that appears after "Set-e", once the return value is nonzero, the entire script exits immediately. Some people like to use this parameter for the sake of ensuring the security of the code. But sometimes this kind of good intentions can lead to serious problems.

Real Case:
The script a.sh starts with "SET-E" and works correctly. After a few months or more, a 3-line Hadoop operation was added to the script due to a requirement upgrade:
#!/bin/bash
Set-e
...
/home/work/.../hadoop Dfs-rmr/app/.../dir
/home/work/.../hadoop Dfs-mkdir/app/.../dir
/home/work/.../hadoop Dfs-put file_1/app/.../dir/
...
These lines of Hadoop command logic are simple: clear and create a new directory on HDFs, and push a local file to this directory for later use. Take these lines out of the command line execution, in addition to the prompt to delete the directory does not exist, and there is no problem, the file will be pushed to the specified place.

But the first time the script was executed, it failed to exit, and the program that called the script was exited, causing serious consequences. The reason is that there is no such directory on HDFs, RMR this line will return 255, this value is captured by "set-e" in front of the script, which directly causes the script to exit.

The new code itself is not a problem, first delete and then create a new directory, but to ensure that the data security of the comparison of the operation, the deletion of the command itself fault tolerance, you can ensure that subsequent commands to execute normally. The fact is that the script has hundreds of lines, and the logic is more complex, in addition to these lines of code, the developer has no recollection of this script there is a "set-e" ambush.

The visible setting "SET-E" may be helpful during script development, and after development, especially for scripts that may have an upgrade later, it may be a security risk.

Shell Script Set+e Set-e

Related Article

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.