I'd like a shell-script syntax checker that can detect at least the following errors, and more:
Variable $VAR used but VAR has not been defined.
Variable VAR defined but never used.
Use of unquoted variables which might break external commands e.g. SOMETHING in: value=`grep $SOMETHING file`
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)?
-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 , 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
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
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.
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.
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.