Linux 如何在不运行 Bash 脚本的情况下对其进行语法检查?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/171924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I syntax check a Bash script without running it?
提问by Tom Feiner
Is it possible to check a bash script syntax without executing it?
是否可以在不执行的情况下检查 bash 脚本语法?
Using Perl, I can run perl -c 'script name'
. Is there any equivalent command for bash scripts?
使用 Perl,我可以运行perl -c 'script name'
. bash 脚本是否有任何等效的命令?
采纳答案by andy
bash -n scriptname
Perhaps an obvious caveat: this validates syntax but won't check if your bash script tries to execute a command that isn't in your path, like ech hello
instead of echo hello
.
也许一个明显的警告:这会验证语法,但不会检查您的 bash 脚本是否尝试执行不在您的路径中的命令,例如ech hello
代替echo hello
.
回答by Jeevan
sh -n script-name
Run this. If there are any syntax errors in the script, then it returns the same error message.
If there are no errors, then it comes out without giving any message. You can check immediately by using echo $?
, which will return 0
confirming successful without any mistake.
运行这个。如果脚本中有任何语法错误,则返回相同的错误消息。如果没有错误,那么它会在不给出任何消息的情况下出现。您可以使用 立即检查echo $?
,它会返回0
确认成功,没有任何错误。
It worked for me well. I ran on Linux OS, Bash Shell.
它对我很有效。我在 Linux 操作系统、Bash Shell 上运行。
回答by Diego Tercero
I also enable the 'u' option on every bash script I write in order to do some extra checking:
我还在我编写的每个 bash 脚本上启用了 'u' 选项,以便进行一些额外的检查:
set -u
This will report the usage of uninitialized variables, like in the following script 'check_init.sh'
这将报告未初始化变量的使用情况,如以下脚本“check_init.sh”
#!/bin/sh
set -u
message=hello
echo $mesage
Running the script :
运行脚本:
$ check_init.sh
Will report the following :
将报告以下内容:
./check_init.sh[4]: mesage: Parameter not set.
./check_init.sh[4]: 消息:未设置参数。
Very useful to catch typos
非常有用的捕捉错别字
回答by dvd818
Time changes everything. Here is a web sitewhich provide online syntax checking for shell script.
时间会改变一切。这是一个为 shell 脚本提供在线语法检查的网站。
I found it is very powerful detecting common errors.
我发现它在检测常见错误方面非常强大。
About ShellCheck
关于 ShellCheck
ShellCheckis a static analysis and linting tool for sh/bash scripts. It's mainly focused on handling typical beginner and intermediate level syntax errors and pitfalls where the shell just gives a cryptic error message or strange behavior, but it also reports on a few more advanced issues where corner cases can cause delayed failures.
ShellCheck是一个用于 sh/bash 脚本的静态分析和 linting 工具。它主要专注于处理典型的初级和中级语法错误和陷阱,其中 shell 只是给出一个神秘的错误消息或奇怪的行为,但它也报告了一些更高级的问题,这些问题可能导致延迟失败。
Haskell source code is available on GitHub!
Haskell 源代码在 GitHub 上可用!
回答by mug896
null command [colon] also useful when debugging to see variable's value
null 命令 [colon] 在调试以查看变量值时也很有用
set -x
for i in {1..10}; do
let i=i+1
: i=$i
done
set -
回答by Cengiz
There is BashSupport pluginfor IntelliJ IDEAwhich checks the syntax.
有BashSupport插件的IntelliJ IDEA的该检查语法。
回答by Gerald Hughes
I actually check all bash scripts in current dir for syntax errors WITHOUT running them using find
tool:
我实际上检查了当前目录中的所有 bash 脚本是否存在语法错误,而没有使用find
工具运行它们:
Example:
例子:
find . -name '*.sh' -exec bash -n {} \;
find . -name '*.sh' -exec bash -n {} \;
If you want to use it for a single file, just edit the wildcard with the name of the file.
如果要将它用于单个文件,只需编辑带有文件名的通配符。
回答by Elvis Ciotti
If you need in a variable the validity of all the files in a directory (git pre-commit hook, build lint script), you can catch the stderr output of the "sh -n" or "bash -n" commands (see other answers) in a variable, and have a "if/else" based on that
如果您需要在一个变量中确定目录中所有文件的有效性(git pre-commit hook,构建 lint 脚本),您可以捕获“sh -n”或“bash -n”命令的 stderr 输出(参见其他答案)在一个变量中,并有一个基于它的“if/else”
bashErrLines=$(find bin/ -type f -name '*.sh' -exec sh -n {} \; 2>&1 > /dev/null)
if [ "$bashErrLines" != "" ]; then
# at least one sh file in the bin dir has a syntax error
echo $bashErrLines;
exit;
fi
Change "sh" with "bash" depending on your needs
根据您的需要将“sh”更改为“bash”