如何调试bash脚本? [英] How to debug a bash script?

查看:96
本文介绍了如何调试bash脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法来调试bash脚本?例如一些打印一种执行日志,如呼叫线路1号,主叫用户线2等。


解决方案

 上海-x脚本[ARG1 ...]
bash的-x脚本[ARG1 ...]

这些给你的正在执行什么痕迹。 (参见澄清接近答案的底部。)

有时候,你需要控制脚本中的调试。在这种情况下,作为 Cheeto 提醒我,你可以使用:

 设置-x

本打开的调试上。您可以然后再关闭:

 一套+ X

(您可以通过分析找出当前跟踪状态 $ - ,当前的旗帜,为 X 。 )

此外,炮弹一般提供的选项 -n '的'不执行和 -v '的详细模式;您可以使用这些组合看外壳是否认为它可以执行你的脚本 - 如果你有一个报价不平衡的地方偶尔有用


有争,在猛砸 -x 选项是从其他shell不同(见注释)。该猛砸手册说:


  • -x

    打印简单命令的痕迹,命令,情况命令,选择命令和算术命令及其参数或相关联的单词列表后,便展开和执行之前。在 PS4的值变量被扩展而得到的值的命令及其参数扩展之前打印。


这多少似乎并没有表示不同的行为的。我看不出在手册中以 -x 其他相关的参考资料。它没有描述在启动序列差异。

澄清的:在系统如一个典型的Linux中,其中 / bin / sh的'是一个符号链接 /斌/庆典(或其它地方Bash的可执行文件中),这两个命令行实现运行与执行跟踪脚本的同等效力。在其他系统(例如,Solaris和Linux的一些更现代的变种), / bin / sh的是不是bash,而两行命令会给(略)不同结果。最值得注意的是, / bin / sh的'将在bash结构,它不承认在所有的混淆。 (在Solaris中, / bin / sh的是一个Bourne shell;在现代的Linux,它有时短跑 - 一个更小,更严格的POSIX只壳)。当通过名称调用像这样的'家当'线(#!/斌/庆典'VS '#!/ bin / sh的')在文件的开始对内容如何间preTED没有影响

Bash的手册对猛砸POSIX模式一个部分,它,与此相反的答案由来已久,但错误的版本(见下面的评论),在广泛的细节之间的差别猛砸援引为 SH 和确实描述巴什援引为庆典

在调试(bash)的shell脚本,这将是明智和理智的 - 必要的,即使 - 使用与 -x 选项家当行中指定的外壳。否则,你可能(会?)运行脚本时调试时获得不同的行为。

Is there any way to debug a bash script? E.g something that prints a sort of execution log like "calling line 1", "calling line 2" etc.

解决方案

sh -x script [arg1 ...]
bash -x script [arg1 ...]

These give you a trace of what is being executed. (See also 'Clarification' near the bottom of the answer.)

Sometimes, you need to control the debugging within the script. In that case, as Cheeto reminded me, you can use:

set -x

This turns debugging on. You can then turn it off again with:

set +x

(You can find out the current tracing state by analyzing $-, the current flags, for x.)

Also, shells generally provide options '-n' for 'no execution' and '-v' for 'verbose' mode; you can use these in combination to see whether the shell thinks it could execute your script — occasionally useful if you have an unbalanced quote somewhere.


There is contention that the '-x' option in Bash is different from other shells (see the comments). The Bash Manual says:

  • -x

    Print a trace of simple commands, for commands, case commands, select commands, and arithmetic for commands and their arguments or associated word lists after they are expanded and before they are executed. The value of the PS4 variable is expanded and the resultant value is printed before the command and its expanded arguments.

That much does not seem to indicate different behaviour at all. I don't see any other relevant references to '-x' in the manual. It does not describe differences in the startup sequence.

Clarification: On systems such as a typical Linux box, where '/bin/sh' is a symlink to '/bin/bash' (or wherever the Bash executable is found), the two command lines achieve the equivalent effect of running the script with execution trace on. On other systems (for example, Solaris, and some more modern variants of Linux), /bin/sh is not Bash, and the two command lines would give (slightly) different results. Most notably, '/bin/sh' would be confused by constructs in Bash that it does not recognize at all. (On Solaris, /bin/sh is a Bourne shell; on modern Linux, it is sometimes Dash — a smaller, more strictly POSIX-only shell.) When invoked by name like this, the 'shebang' line ('#!/bin/bash' vs '#!/bin/sh') at the start of the file has no effect on how the contents are interpreted.

The Bash manual has a section on Bash POSIX mode which, contrary to a long-standing but erroneous version of this answer (see also the comments below), does describe in extensive detail the difference between 'Bash invoked as sh' and 'Bash invoked as bash'.

When debugging a (Bash) shell script, it will be sensible and sane — necessary even — to use the shell named in the shebang line with the -x option. Otherwise, you may (will?) get different behaviour when debugging from when running the script.

这篇关于如何调试bash脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆