Linux Shell Script Series Tutorial (vii): script Debugging _linux Shell

Source: Internet
Author: User

First, debug scripts

Debugging is one of the important features that every programming language should implement, and when something unexpected happens, use it to generate the running information of the script, and the debugging information can help you figure out what caused the program to crash or behave abnormally.
Second, bash built-in debugging function

With the built-in debugging capabilities of bash, you can debug the entire script or only some of the statements in the script.
#使用set-X and set +x to debug a sentence of a script

Copy Code code as follows:

#!/bin/bash
For i in {1 2 3 4 5 6};
Todo
Set-x #开启调试功能
echo $i #要调试的语句
Set +x #关闭调试功能
Done
echo "Script executed."

Copy Code code as follows:

#使用-x option to debug the entire script
Bash-x script.sh #等价于sh-X script.sh

Three, custom debugging function

Bash's built-in debugging capabilities only output fixed-format debugging information, but in many cases we need to display debugging information in a custom format, which can be built by _DEBUG environment variables.

Copy Code code as follows:

#使用_DEBUG =on Bash script.sh run the following script
#!/bin/bash
function DEBUG ()
{
["$_debug" = "on"] && $@ | | : #使用_DEBUG环境变量调试
}
For i in {1 2 3}
Todo
DEBUG Echo $i
Done

We are in front of every statement that needs to print debug information, and debug information will not print if _debug=on is not passed a script. In bash,: Tell the shell not to do anything.

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.