Study notes: set -e shell in, set + e usage

Study notes: set -e shell in, set + e usage

https://blog.csdn.net/xiaofei125145/article/details/39345331/

set -e # Exit the script if an error happens

Screenplay A

set +e # don't bail out of bash script if ccache doesn't exist

B script

set -e # back to regular "bail out on error" mode

Script C

 

As above, a script shell,

When A and C script execution if there is a return value is nonzero, the whole script exits immediately

If a B appears in the script returns a nonzero value will continue to execute the following script C.

 

-e set
-e parameter set command, linux comes as follows:
. "Immediately the Exit exits with IF A Simple Command Status A non-ZERO"
That is, after "set -e" code appears, the event the return value is non-zero, the whole script exits immediately. Some people like to use this parameter, out of consideration to ensure the safety code. But sometimes, this beautiful mind, can lead to serious problems.

Real case:
the beginning of the script a.sh use "set -e", and can run normally. After a few months or longer, due to the demand to upgrade and increase the three lines in the script hadoop operation:
# / bin / bash!
The SET -e
...
/home/work/.../hadoop the DFS -rmr / App /.../dir
/home/work/.../hadoop the DFS -mkdir /app/.../dir
/home/work/.../hadoop the DFS -put file_1 /app/.../dir/
...
these lines hadoop command logic is simple: Clear and create a new directory on hdfs, and push the files to a local directory for later use. These lines alone carry out, execute the command line, in addition to suggesting to delete the directory does not exist, and there is no problem, the file will still be pushed to the designated place.

But when the first execution of the script, but pulled out of the failure, and lead to the overall program calls the script exits, causing serious consequences. The reason is not yet on hdfs this directory, rmr this line returns 255, this value is in front of the script "set -e" to capture, a direct result of the script exits.

The new code itself is not the problem, delete and then create a new directory, but is fairly standard to ensure data security operation, fault tolerance delete command itself, can guarantee the normal execution of subsequent commands. The fact is that there are several hundred lines of the script, and the logic is more complex, increasing the time these lines of code, developers do not remember the script there is a "set -e" the ambush.

Visible Set "set -e", may be helpful in the script development process, and after the development is completed, in particular, may have to upgrade script for the latter, it may be buried a security risk.
----------------
Disclaimer: This article is the original article CSDN bloggers "water dripping into the River", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and this link statement.
Original link: https: //blog.csdn.net/xiaofei125145/article/details/39345331/

 

 

 

 

 

Published 80 original articles · won praise 38 · views 40000 +

Guess you like

Origin blog.csdn.net/kuangben2000/article/details/104388545