调试Linux Bash脚本

[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]

LEARN MORE  Python Set Data Structure With Examples
通过示例了解更多Python设置数据结构

翻译自: https://www.poftut.com/debugging-linux-bash-scripts/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值