Shell syntax checker available, or is a new project needed?

I'd like a shell-script syntax checker that can detect at least the following errors, and more:

  1. Variable $VAR used but VAR has not been defined.
  2. Variable VAR defined but never used.
  3. Use of unquoted variables which might break external commands e.g. SOMETHING in: value=`grep $SOMETHING file`
  4. Unreachable code. E.g. after an exit.

When people ask for a syntax checker they seem to always just get told "use bash -n", but this doesn't do any of the above and isn't a true syntax checker like, say, pychecker.

Is there one out there (or is a new open-source project needed)?

Hi.

I think the advice you cited is correct.

              -n      Read commands but do not execute them.  This may be used
                      to  check  a  shell  script  for syntax errors. 
-- excerpt from man bash

I may be using Wiktionary definition 3 in this response :slight_smile: , but I think you desire a code in the vein of definition 2.

If I have some time, I'll poke around to see if I can find is anything along those lines.

It's possible that if you have such complex shell scripts that you need a semantic checker, then you may be at the point to consider writing the solution in a more appropriate language (or structure) ... cheers, drl

PS The code

set -o nounset

will diagnose undefined variables.

It might be nice if large shell scripts didn't exist, but they do, and if you have to maintain other people's then you have no choice :slight_smile:

I should stress that running the script with "debug" options on is not the same as syntax checking. E.g. what if the code path with the error doesn't get followed when I run it?

What I desire is something like "lint", or "pychecker", or even the "javac" compiler, which spots errors without actually being dependent on the return values of any commands in the script.

Hi.

I ran across one paper (PDF) that discusses semantic checking in bash scripts: http://www.cis.upenn.edu/~stevez/papers/MZ07.pdf -- one of a number of hits from a Google search.

I did not see anything in the paper that was a link to the code -- 4000+ lines of OCaml -- but you could drop the authors a line to see if they would release it.

Good luck and keep us posted ... cheers, drl

I do not have bash handy but for ksh which is very similar -

/home/jmcnama> set +o
Current option settings
allexport        on
bgnice           on
emacs            off
errexit          off
gmacs            off
ignoreeof        off
interactive      on
keyword          off
markdirs         off
monitor          on
noexec           on
noclobber        off
noglob           off
nolog            off
nounset          on
privileged       off
restricted       off
trackall         off
verbose          off
vi               on
viraw            off
xtrace           off

If nounset is on you will get what you requested, errors on undefined variables, and error checking for syntax only with noexec on.

The above doesn't cover any code which isn't run during that particular test, such as anything the other side of an if condition:

#!/usr/bin/bash
set -o nounset

if [ -f /dev/null ] ; then
    echo $NO_SUCH_VAR
else
    echo $NEVER_HEARD_OF_IT
fi

Run to spot first unspecified variable:

bash ./broken.sh
./broken.sh: line 8: NEVER_HEARD_OF_IT: unbound variable

But what about the other one? (Trying with -n option)

bash -n ./broken.sh

I don't see a means of finding the problem with $NO_SUCH_VAR.

I suspect a new tool is needed, or at least a plugin to an existing tool.