Shell Debugging Technology

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.