Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I program CLI utilities in bash to automate much of my work as a DBA.

I would like improve my code and make it more robus,t maybe with the help of some static code analysis tool like the one I used to use for Java.

Does any of you use a static code analysis tool for bash scripts ?

Is so, which one do you use ?

Does one exist ?

share|improve this question
    
How would static analysis work? Can you detect cat abuse? (cat file | grep foo) - or the nuances of poor awk or sed invocations? Have you considered perl, and perl critic? –  MichaelT Jun 18 '13 at 18:29
1  
When Bash becomes difficult to maintain, I turn to Perl. It provides some compile-time checking and type guarantees while also fitting comfortably into the same problem domain as shell scripts. Just food for thought. –  Jon Purdy Jun 18 '13 at 18:30
1  
@MichaelT It would check style, smells, best practices, etc. Shell scripts have variables, control structures, etc, not only commands. I use shell scripting because it is ubiquitous. Also I have almost everything already automated with shell scripts. –  user61852 Jun 18 '13 at 18:35
    
@JonPurdy I need something that is already on very old servers. –  user61852 Jun 18 '13 at 18:40
    
@user61852 I cannot think of any. PMD works by transforming the code to an abstract symbol tree, represented by xml - and then running xpath searches against it. However, that implies transforming into an AST. But when one has invocations and pipes as part of the code (cut, awk, sed, grep, sort, uniq, etc...) how does one analize all of those possibilities too? –  MichaelT Jun 18 '13 at 18:41

1 Answer 1

up vote 5 down vote accepted

shellcheck is an online tool which you can also build locally, and shlint which also runs checkbashisms for you. Perl is an excellent alternative to Bash for larger scripts, especially with use warnings; to enable compile-time warnings and use strict; to enforce things like variable scope and function signatures.

share|improve this answer
    
Thanks ! I checked Shellcheck. That's precisely what I was looking for...but I want to use it locally and the compilation process seems a little complicated for me. Where could I find a binary for Ubuntu ? –  user61852 Jun 18 '13 at 20:00
    
@user61852: The process shouldn’t be that bad. sudo apt-get install haskell-platform && cabal update && git clone https://github.com/koalaman/shellcheck.git && cd shellcheck && cabal install --only-dependencies && cabal configure && cabal build ought to do it. –  Jon Purdy Jun 19 '13 at 20:26
1  
sudo apt-get install haskell-platform wants to install like a hundred packages... too invasive for me. –  user61852 Jun 19 '13 at 20:48
    
@user61852: Yes, unfortunately for your purposes, Haskell Platform is pretty large; it includes the whole GHC toolchain and standard Haskell libraries. Of course, it is just a one-time thing—up to you. –  Jon Purdy Jun 19 '13 at 22:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.