是否有像Lint或Perl :: Critic这样的静态分析工具可用于shell脚本? [英] Is there a static analysis tool like Lint or Perl::Critic for shell scripts?

查看:71
本文介绍了是否有像Lint或Perl :: Critic这样的静态分析工具可用于shell脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何外壳程序(特别是bash或ksh)检查程序测试外壳程序脚本的样式,最佳实践,命名约定等? (类似于Lint for C或Perl :: Critic for Perl。)

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.)

我知道使用ksh可以通过运行 ksh来进行语法检查- n script.ksh ,但我希望不仅限于语法检查-还可以解析/分析实际代码?

I know with ksh you can do syntax checking by running ksh -n script.ksh but I was hoping for something more than just sytax checking - something that parses/analyzes the actual code?

推荐答案

Debian和Ubuntu项目使用脚本< a href = http://man.he.net/man1/checkbashisms rel = nofollow noreferrer> checkbashisms ,用于查找特定的模式可能表明有人依赖 / bin / sh bash

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.

除此之外,大多数外壳程序都有 -n 选项来分析和报告错误。您可以针对几种不同的shell检查脚本,以确保其仅使用可移植的语法:

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

要添加的编辑:自从编写此答案以来,在稍后的答案中建议,已编写了noreferrer> shellcheck 。与以前的建议相比,在执行shell脚本方面做得更加彻底。

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.

这篇关于是否有像Lint或Perl :: Critic这样的静态分析工具可用于shell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆