如何调试 Bash 脚本? [英] How can I debug a Bash script?

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

问题描述

有没有办法调试 Bash 脚本?

Is there a way to debug a Bash script?

例如,打印某种执行日志的东西,如调用第 1 行"、调用第 2 行"等.

E.g., something that prints a sort of an 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.)

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

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

(您可以通过分析$-,当前标志,为x找出当前跟踪状态.)

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

此外,shell 通常为不执行"提供选项 '-n',为 'verbose' 模式提供选项 '-v';你可以结合使用这些来查看 shell 是否认为它可以执行你的脚本——如果你在某处有不平衡的引用,偶尔会很有用.

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.

有人争论说 Bash 中的 '-x' 选项与其他 shell 不同(请参阅注释).Bash 手册 说:

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

  • -x

打印简单命令、for 命令、case 命令、select 命令和算术 for 命令的痕迹命令及其参数或相关单词列表在展开之后和执行之前.PS4 变量的值被扩展,结果值打印在命令及其扩展参数之前.

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.

这么多似乎根本不代表不同的行为.我在手册中没有看到任何其他对-x"的相关引用.它没有描述启动顺序的差异.

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.

澄清:在典型的 Linux 机器等系统上,/bin/sh"是/bin/bash"的符号链接>'(或任何可以找到 Bash 可执行文件的地方),这两个命令行实现了在执行跟踪打开的情况下运行脚本的等效效果.在其他系统(例如 Solaris 和一些更现代的 Linux 变体)上,/bin/sh 不是 Bash,两个命令行会给出(略有)不同的结果.最值得注意的是,'/bin/sh' 会被 Bash 中根本无法识别的结构混淆.(在 Solaris 上,/bin/sh 是一个 Bourne shell;在现代 Linux 上,它有时是 Dash — 一个更小、更严格的仅 POSIX 的 shell.)当这样调用时,'shebang文件开头的 ' 行 ('#!/bin/bash' vs '#!/bin/sh') 对内容的解释方式没有影响.

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.

Bash 手册有一节介绍了 Bash POSIX 模式 与此答案的长期但错误的版本相反(另请参见下面的评论),它确实详细描述了Bash 调用为 sh"和Bash"之间的区别调用为 bash'.

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'.

在调试(Bash)shell 脚本时,使用带有-x 选项的shebang 行中命名的shell 将是明智和理智的——甚至是必要的.否则,您可能(会?)在调试时获得与运行脚本时不同的行为.

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天全站免登陆