Application of set -x set +x set -e set -u set -n in Linux shell script

                                        Application of set -x set +x set -e set -u set -n in Linux shell script

The set command can define the running mode of the script, the way of obtaining variables, the execution process of the script, and the test of the script.

1, set -u (check the variables in the script, if any variables are not defined, the script will be terminated) (self-checking function of the script)

#!/bin/bash
set -u
echo $A

This is a very simple shell script whose function is to output the value of the variable abc, but you can see that the value of the variable a is not given in the script. If it is executed, an error will be reported. The script name is test.sh. The output after execution is as follows (that is, the variable is not assigned):

[root@centos7 ~]# bash test.sh 
test.sh: line 3: A: unbound variable



If you don't use set -u, the script will ignore the error, execute it completely, and will not report an error, which means that the default is not to check whether the variable is assigned.

[root@centos7 ~]# cat test.sh 
#!/bin/bash
echo $A
[root@centos7 ~]# bash test.sh 

[root@centos7 ~]# 

2,set -e

When set -e is not used, the script can continue to execute even if it encounters an error, for example:

#!/bin/bash
cat hello.sh
echo "this is my script file"

 

[root@centos7 ~]# cat test.sh 
#!/bin/bash
cat hello.sh
echo "this is my script file"
[root@centos7 ~]# bash test.sh 
cat: hello.sh: No such file or directory
this is my script file

 

Obviously, I wrote a cat file casually. Of course, this file must not exist. The third line is still executed. If you are writing other scripts, I hope that the script will encounter a non-zero return value, which is an error. Exit, then, please add set -e to the first line of the script (this can be regarded as a debug mode opening command).

[root@centos7 ~]# cat test.sh 
#!/bin/bash
set -e
cat hello.sh
echo "this is my script file"
[root@centos7 ~]# bash test.sh 
cat: hello.sh: No such file or directory

 

As you can see, it is equivalent to hitting a debug breakpoint and stopping the execution of the script when encountering an error, so it is not difficult to imagine that set -e can be regarded as a must for installation scripts. (If there is an error in the previous installation, then the subsequent execution is meaningless, and the script needs to be very robust)

3,set -o pipefail

Detect the combined commands in the script—the debug mode of the pipeline command, that is, you need to start the debug mode with set -e first, and then check the robustness of the shell script for the pipeline command separately

As shown in the second subsection, the script is now changed to the following, do you think it will be executed to the end?

[root@centos7 ~]# cat test.sh 
#!/bin/bash
set -e
cat hello.sh |echo "second command is success!"
echo "this is my script file"
[root@centos7 ~]# bash test.sh 
second command is success!
cat: hello.sh: No such file or directory
this is my script file

It can be seen that the front part of the pipeline command cat hello.sh failed, and the subsequent echo "second command is success!" was successful, because the pipeline command was indeed successfully executed. Therefore, the shell script believes that the second line of command is successful as a whole , The return value is 0, and the third line of commands can continue. At this point, even if there is set -e, no error can be detected. This is definitely not what we want (the content of the script execution is not accurate, right? ~~)



Add set -o pipefail to the above script, the script content and execution result are as follows:

[root@centos7 ~]# cat test.sh 
#!/bin/bash
set -e
set -o pipefail
cat hello.sh |echo "second command is success!"
echo "this is my script file"
[root@centos7 ~]# bash test.sh 
second command is success!
cat: hello.sh: No such file or directory

As you can see, the script is executed at the second line, and the correct part of the pipeline command is still executed, but before the error is reported.

4. Set -x and set +x 

Why are these two put together? Because this is actually a switch that displays the execution process of the script and exposes the value of the variables in the script. -x is on, and +x is equal to the default off. In general, the script closes the display process.

[root@centos7 ~]# cat test.sh 
#!/bin/bash
set -e
set -x
set -o pipefail
echo $PWD
cat hello.sh |echo "second command is success!"
echo "this is my script file"
[root@centos7 ~]# bash test.sh 
+ set -o pipefail
+ echo /root
/root
+ cat hello.sh
+ echo 'second command is success!'
second command is success!
cat: hello.sh: No such file or directory

In the above script, after adding set -x, we can clearly see the value of the variable $PWD, right? ? After execution, I didn't see any PWD, but we know that the value of $PWD is /root. If you delete the set -x line or rewrite it as set +x, you will not see the execution of the script.

To summarize:

The set -u mode is specifically for variables. If there are unassigned variables, it usually means redundancy and invalidity for the program. This is not what we want. (If the shell uses a lot of variables), or, a variable is empty, and the rm -rf variable in the script, at this time, set -u will protect you, because if it is empty and there is no- u, rm -rf command will delete everything, at this time, you set set -u may save your life! ! !

set -e debug breakpoint mode, this is often used in the flow control of the script, for example, a certain script, do not want to see any errors (return value is non-zero indicates an error), because this error is very important for the script work, such as The installation scripts are all wrong before, and the following lines are still being executed, which may cause catastrophic consequences, which will greatly improve the robustness of the script.

The set -o pipefail pipeline command participates in the debug breakpoint mode. By default, the shell considers the pipeline command as a whole and is related to |. Set -o pipefail is changed to and or ||, that is, the pipeline command participates.

set -x displays the execution process of the script and displays the result of the script's processing of variables. If a script uses a lot of variables, and we want to see whether these variables are passed and used correctly, then set -x will be a good choice for you. (Quickly locate problems, especially those caused by variables)

In summary, set -ue and set -o pipefail can ensure the robustness of shell scripts! ! ! set -x can provide you with visual variable value checking. If there are dangerous commands, such as> redirect, rm -rf delete, these, please try to use these sets.

In the script, set -x is equal to when the script is executed, bash -x script name or source -x script name

 

 

 

 

Guess you like

Origin blog.csdn.net/alwaysbefine/article/details/114187380
set
set