How to debug shell scripts with built-in variables _linux shell

Source: Internet
Author: User

The general shell script debugging is basically echo to deal with the larger script when, it is more trouble, out of the question, is not very good to locate which line of code problems.
In fact, some of the variables built into the shell can be a good solution to this problem:
$LINENO $FUNCNAME $BASH _lineno These variables record the current execution position of the script and the function being executed. You can specify the man document page.

Instance code: a.sh

Copy Code code as follows:
#!/bin/bash
ABC () {
echo "Wo shi abc ()"
echo "func: $FUNCNAME ln: $LINENO ln2:${bash_lineno[1]} brother: ${funcname[1]}"
}

B.sh:
Copy Code code as follows:
#!/bin/bash
.. /a.sh
Abc
CDF () {
Abc
}


Execution results:
[Root@node2 ~]#./b.sh
[Code]wo shi ABC ()
FUNC:ABC Ln:5 ln2:0 Brother:main
Wo shi abc () FUNC:ABC Ln:5 ln2:9 BROTHER:CDF
I wrote in the b.sh. The CDF function calls the ABC function, see the output difference, we can use these parameters to print out the location of the code error line, and the code error, which function called, and so on ....

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.