开发者

Is there a static analysis tool like Lint or Perl::Critic for shell scripts?

Are there any shell (specifically bash or ksh) checkers that test shell scripts for style, best practices, naming conventions, etc? (Something like Lint for C, or Perl::Critic for Perl.)

I know with ksh you can do syntax checking by running ksh -n script.ksh but I was hoping for something more than j开发者_运维技巧ust sytax checking - something that parses/analyzes the actual code?

I'm probably out of luck, but I guess it doesn't hurt to ask.


I found shellcheck: it tests for common errors in quoting and other things you overlook ("because it works").


The Debian and Ubuntu projects use a script checkbashisms, that looks for particular patterns that might indicate that someone is relying on /bin/sh being bash.

Beyond that, most shells have a -n option to parse and report errors. You could check your script against several different shells to make sure it uses only portable syntax:

for shell in zsh ksh bash dash sh
do
  echo "Testing ${shell}"
  ${shell} -n my_script.sh
done

edit to add: Since writing this answer, shellcheck has been written, as suggested in a later answer. This does a much more thorough job of linting shell scripts than the previous suggestions.


I wrote shlint to wrap checkbashims and the other basic linting options available to shells presented in Brian's answer.

It's installable via rubygems (gem install shlint) as sort of a gag, really, but you only need perl (for checkbashims) and a POSIX compliant shell to interpret the shlint command itself. Should work out of the box on OSX and Ubuntu.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜