[rps-include post=6835]
[rps-include post = 6835]
While writing bash scripts we generally expects some results and provide these results to other scripts. But things goes not well as we expect. So we need to see under the hood and what is happening while script is running. Debugging is inspecting bash scripts to see what is wrong with script or show detailed information about script run.
在编写bash脚本时,我们通常会期望一些结果并将这些结果提供给其他脚本。 但是事情进展不如我们预期。 因此,我们需要深入了解脚本运行时发生的情况。 调试正在检查bash脚本,以查看脚本出了什么问题或显示有关脚本运行的详细信息。
调试整个脚本 (Debugging Whole Script)
To make whole script debug mode use -x parameter when calling the script
要使整个脚本调试模式在调用脚本时使用-x参数
$ bash -x run.sh
+ hostname -I
10.101.61.12 192.168.122.1 172.17.0.1
Our script is simple just issue host name -I command. As we can see that issued command is echoed to the console with +sign.
我们的脚本很简单,只需发出主机名-I命令即可。 如我们所见,已发出的命令以+号回显到控制台。
调试脚本的某些部分 (Debugging Some Part of Script)
Debugging is useful even we have no problem with the script. It can give information about execution of the script. For long and big script files it may become hard to look debug. Some part of the bash script can be made debug-able like below.
即使脚本没有问题,调试也很有用。 它可以提供有关脚本执行的信息。 对于较长的大型脚本文件,调试起来可能会很难。 可以将bash脚本的某些部分设置为可调试,如下所示。
#!/bin/bash
set -x
echo "Debug is on"
set +x
echo "Debug is off"
This is our script where we set on debug with set -x and then set off with set +x.
这是我们的脚本,在其中我们使用set -x进行调试,然后使用set + x进行调试。
$ ./run.sh
+ echo 'Debug is on'
Debug is on
+ set +x
Debug is off
After activating debug we can see the script command but after disabling debugging we can only see script output and cannot see script commands.
激活调试后,我们可以看到脚本命令,但是禁用调试后,我们只能看到脚本输出,而看不到脚本命令。
[rps-include post=6835]
[rps-include post = 6835]