linux set -e

set -e

The -e parameter of the set command, the description that comes with linux is as follows:
"Exit immediately if a simple command exits with a non-zero status."
That is to say, the code that appears after "set -e", once the return value appears Non-zero, the entire script will exit immediately .

 

It is recommended that every script you write should add set -e at the beginning of the file.

This statement tells bash to exit if the result of any statement is not true . The benefit of this is to prevent errors from snowballing into a fatal error that should have been handled before.

 

Description:
set -e open e mode
set +e close e mode


Test example:

 

test script 1

#!/bin/bash
echo "1"
mkdir aaa
echo "2"

 

First execution:
root@bosh # ./test
1
2

 

Second execution:
root@bosh # ./test
1
mkdir: cannot create directory 'aaa': File exists
2

 

Description: In the bash interpreter, by default, a certain line of the script is unsuccessfully executed, which will not affect the continued execution of the script.


test script 2

On the basis of script 1, add set -e

 

#!/bin/bash
set -e
echo "1"
mkdir aaa
echo "2"

 

Execute test script 2
root@bosh: # ./test
1
mkdir: cannot create directory 'aaa': File exists

 

Note: After the e mode is turned on, after the execution of mkdir aaa fails, the subsequent echo 2 is no longer executed.


test script 3

On the basis of script 2, after set -e, append set +e

#!/bin/bash
set -e
echo "1"
#close the e parameter
set +e
mkdir aaa
echo "2"

 

 Execute test script 3

root@bosh:./test
1
mkdir: cannot create directory ‘aaa’: File exists
2

 

Note: After the e mode is turned off, if mkdir aaa fails, it will not affect the continuation of subsequent code execution.


About the set
command The function of the set command is to display the existing shell variables in the system and set the new variable values ​​of the shell variables. When using set to change shell properties, the symbols "+" and "-" are used to turn on and off the specified mode, respectively. The set command cannot define new shell variables. If you want to define a new variable, you can use the declare command to define it in the format of variable name=value.

 

Available parameters of the set command
-a: Mark the modified variable for output to environment variables.
-b: Causes the suspended daemon to report the execution status immediately.
-C: The file generated by the redirection cannot overwrite the existing file.
-d: By default, the Shell will use a hash table to memorize used instructions to speed up the execution of instructions. Use the -d parameter to cancel.
-e: If the value returned by the command is not equal to 0, exit the shell immediately.
-f: Suppress the use of wildcards.
-h: Automatically record the location of the function.
-H Shell: You can use "!" plus <command number> to execute the command recorded in the history.
 -k: The parameters given by the command will be regarded as the environment variables of this command.
 -l: Log the variable name of the for loop.
  -m: Use monitor mode.
  -n: Only read instructions without actually executing them.
  -p: Start priority mode.
  -P: After the -P parameter is enabled, the symbolic link will be replaced by the actual file or directory when the command is executed.
 -t: After executing the following instructions, exit the shell.
 -u: Display an error message when an undefined variable is used during execution.
 -v: Display the input value read by the shell.
-x: After the command is executed, the command and its parameters will be displayed first.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326989165&siteId=291194637