调试Shell脚本:不执行语法检查


9

我是否可以提供一些选项来仅检查[bash] shell脚本的语法以检查其语法,但实际上不执行任何操作也不造成任何潜在的损害?

Answers:


7

bash(1)手册页:

-n      Read commands but do not execute them. This may be used to check a
        shell script for syntax errors. This is ignored by interactive shells.

明显。我不知道为什么我这么盲目,以至于我在手册页中都看不到这一点。
艾永2011年

1

试用http://www.shellcheck.net

$ shellcheck myscript.sh

    In myscript.sh line 590:
    for f in $*; do
    ^-- SC1009: The mentioned parser error was in this for loop.


    In myscript.sh line 617:
        if [ ! -e "$somefile".vcf ]; then
        ^-- SC1046: Couldn't find 'fi' for this 'if'.
        ^-- SC1073: Couldn't parse this if expression.


    In myscript.sh line 1026:
    done
    ^-- SC1047: Expected 'fi' matching previously mentioned 'if'.
        ^-- SC1072: Unexpected keyword/token. Fix any mentioned problems and try again.

好吧,它并没有告诉我634行上是否缺少'if',但这非常有帮助。


还需要注意的是,您可以在本地下载并运行它,它可以在Haskell中运行。
phk
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.