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

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

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

答案1

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.

答案2

试用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”,但它非常有帮助。

相关内容