sh脚本和bash脚本_如何调试Bash脚本?

sh脚本和bash脚本

How to debug a Bash script if it has some bugs? Common techniques like printing varibles out for checking apply for bash too. For bash, I also use 2 bash-specific techniques.

如果Bash脚本有一些错误,该如何调试 ? 诸如打印变量以进行检查之类的常见技术也适用于bash。 对于bash,我还使用2种特定于bash的技术。

使用errexit选项 (Use errexit option)

Run the script with -e option like bash -e your-script.sh or add set -o errexit to the beginning of the script.

使用-e选项(如bash -e your-script.sh运行脚本,或将set -o errexit添加到脚本的开头。

errexit makes bash exit immediately if one statement’s return code is not 0. This way, you know which statement goes wrong.

如果一个语句的返回码不为0,则errexit会使bash立即退出。这样,您就知道哪个语句出错。

使用xtrace选项 (Use xtrace option)

Run the script with -x option like bash -x your-script.sh or add set -o xtrace to the beginning of the script.

使用-x选项(如bash -x your-script.sh运行脚本,或将set -o xtrace添加到脚本的开头。

The -x option will make bash print statements executed so that you know what is going on.

-x选项将使bash 打印语句执行,以便您了解发生了什么。

一个例子 (One example)

For example,

例如,


#!/bin/bash

set -x

i=0
let i=i+1

echo $i
false # this will stop the script run with -e
date

The execution of that script with bash -e script.sh will print

使用bash -e script.sh执行该脚本将打印


+ i=0
+ let i=i+1
+ echo 1
1
+ false

By combining the 2 techniques together, which statement/step went wrong is printed out on the STDOUT.

通过将两种技术结合在一起,哪个语句/步骤出错了,将在STDOUT上打印出来。

翻译自: https://www.systutorials.com/how-to-debug-a-bash-script/

sh脚本和bash脚本

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值