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

前端 未结 3 686
野趣味
野趣味 2020-12-02 09:51

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::Cri

3条回答
  •  佛祖请我去吃肉
    2020-12-02 10:24

    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.

提交回复
热议问题