![调试 shell 脚本:语法检查而不执行](https://linux22.com/image/1951/%E8%B0%83%E8%AF%95%20shell%20%E8%84%9A%E6%9C%AC%EF%BC%9A%E8%AF%AD%E6%B3%95%E6%A3%80%E6%9F%A5%E8%80%8C%E4%B8%8D%E6%89%A7%E8%A1%8C.png)
我是否可以提供一些选项来仅检查 [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
$ 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”,但它非常有帮助。