These two usages of set are often seen in the shell scripts in the work, but like many things in life, habits lead to neglect until problems arise.
1. 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/bashset-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.
2. Set-o pipefail
for the Pipefail option for the SET command-o parameter, Linux explains this:
"If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a Non-zero status,or Zero if all commands in the pipeline exit successfully. This option was disabled by default. "
When this option is set, the return value of the statement that contains the pipe command becomes the return value of the last pipeline command that returns nonzero. It sounds more around, but it's also simple:
# Test.shset-o Pipefaills./a.txt |echo "HI" >/dev/nullecho $?
run test.sh because the current directory does not exist with the A.txt file, output:
ls:./a.txt: No Such file or directory
1#set Set-o Pipefail, return the first non-0 return value from right to left, that is, the return value of LS 1
Comment outSet-o Pipefailthis line, run again, output:
ls:./ a.txt: No such file or directory
0 # noSet-o Pipefail,Returns the return value of the last pipe command by default
Set command in Linux: "Set-e" and "Set-o Pipefail"