Trap command
Basic format: Trap command SIG1 SIG2 ... sign
There are 3 types of signals that can be captured:
Exit: Exits from the function, or the entire script finishes
ERR: When a command returns a non-0 status code, the command execution is unsuccessful
DEBUG: script Summary before each command executes
Demo DEBUG (typically use this method to view all the execution of the entire program)
#!/bin/bash
Trap ' echo ' before execute line: $LINENO, a= $a, b= $b, c= $c "' DEBUG
A=0
b=2
c=100
While:
Do
if ((a >= 10))
Then
Break
Fi
Let "a=a+2"
Let "b=b*2"
Let "c=c-10"
Done
The results of the operation are as follows:
Demo: EXIT (This method is commonly used to determine whether a function is executed successfully)
#!/bin/bash
Func1 ()
{
echo "This was an correct function"
var=2010
return 0
}
Trap ' echo ' line: $LINENO, var= $var "' EXIT #这个语句表示, if the fun1 below is executed correctly, then var= $var value is shown here (20)
Fun1
Demo: ERR
#!/bin/bash
Func2 ()
{
echo "This was an error function"
var=2010
Return 1
}
Trap ' echo ' line: $LINENO, var= $var "' ERR #这个语句表示, if the bottom fun2 is an error execution, then this shows var= $var value (20)
Func2
Shell Debugging Technology