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“脚本名称”。对于bash脚本是否有相同的命令?
7 个解决方案
#1
300
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。
#2
87
Time changes everything. Here is a web site which provide online syntax checking for shell script.
时间改变了一切。这是一个为shell脚本提供在线语法检查的网站。
I found it is very powerful detecting common errors.
我发现它能很有效地检测常见的错误。
About ShellCheck
ShellCheck is 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上找到!
#3
36
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]: mesage:参数未设置。
Very useful to catch typos
捕捉打字错误非常有用。
#4
22
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。
#5
3
null command [colon] also useful when debugging to see variable's value
空命令[冒号]在调试时也很有用,可以看到变量的值
set -x
for i in {1..10}; do
let i=i+1
: i=$i
done
set -
#6
2
I actually check all bash scripts in current dir for syntax errors WITHOUT running them using find
tool:
实际上,我在没有使用find工具运行语法错误的情况下检查了当前目录中的所有bash脚本:
Example:
例子:
find . -name '*.sh' -exec bash -n {} \;
找到。- 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.
如果您想将它用于单个文件,只需编辑带有文件名的通配符。
#7
1
There is BashSupport plugin for IntelliJ IDEA which checks the syntax.
有BashSupport plugin for IntelliJ IDEA,用于检查语法。
#1
300
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。
#2
87
Time changes everything. Here is a web site which provide online syntax checking for shell script.
时间改变了一切。这是一个为shell脚本提供在线语法检查的网站。
I found it is very powerful detecting common errors.
我发现它能很有效地检测常见的错误。
About ShellCheck
ShellCheck is 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上找到!
#3
36
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]: mesage:参数未设置。
Very useful to catch typos
捕捉打字错误非常有用。
#4
22
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。
#5
3
null command [colon] also useful when debugging to see variable's value
空命令[冒号]在调试时也很有用,可以看到变量的值
set -x
for i in {1..10}; do
let i=i+1
: i=$i
done
set -
#6
2
I actually check all bash scripts in current dir for syntax errors WITHOUT running them using find
tool:
实际上,我在没有使用find工具运行语法错误的情况下检查了当前目录中的所有bash脚本:
Example:
例子:
find . -name '*.sh' -exec bash -n {} \;
找到。- 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.
如果您想将它用于单个文件,只需编辑带有文件名的通配符。
#7
1
There is BashSupport plugin for IntelliJ IDEA which checks the syntax.
有BashSupport plugin for IntelliJ IDEA,用于检查语法。