This article mainly introduced the Linux Shell Script Series tutorial (Seven): script debugging, this article explained the bash built-in debugging function and the custom debugging function and so on content, needs the friend may refer to under
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
The code is as follows:
#!/bin/bash
For i in {1 2 3 4 5 6};
Todo
Set-x #开启调试功能
echo $i #要调试的语句
Set +x #关闭调试功能
Done
echo "Script executed."
The code is 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.
The code is 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.