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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell syntax checker available, or is a new project needed?
# 1  
Old 06-15-2009
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)?
# 2  
Old 06-15-2009
Hi.

I think the advice you cited is correct.
Code:
              -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 Smilie , 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
Quote:
semantic
...
1. Of or relating to semantics or the meanings of words.
2. (web design, of code) Reflecting intended structure and meaning.
3. (of a detail or distinction) Petty or trivial; (of a person or statement) quibbling, niggling.

-- semantic - Wiktionary

PS The code
Code:
set -o nounset

will diagnose undefined variables.
# 3  
Old 06-15-2009
Syntax check != run the program

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.
# 4  
Old 06-15-2009
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
# 5  
Old 06-15-2009
I do not have bash handy but for ksh which is very similar -
Code:
/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.
# 6  
Old 06-15-2009
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:

Code:
#!/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:

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

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

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help Needed with File Checker Korn Shell Script

I am attempting to write a korn shell script that does the following, but I am getting errors. I'm new to korn shell scripting and am not sure what I am doing wrong. Please help with example scripts. Thanks. 1) check for day of the week 2) if day of the week is Monday then check for the... (4 Replies)
Discussion started by: ijmoore
4 Replies

2. Shell Programming and Scripting

Help with Bash Shell Script Spell Checker program.

I have a majority of this problem done but seem to be struggling on the last couple of steps. Here is the whole problem to help you guys get a better understanding. Write a shell script that implements a simple spell checker. The filename you will use for your script will be your Z-id followed... (1 Reply)
Discussion started by: DsmRacer2k14
1 Replies

3. Homework & Coursework Questions

Help Needed Regarding Project in File Locking.

hai guys, I'am a newbie to this forum and to unix as well...as a part of our syllabus i got a project in Unix which i have to do in 4 days.Project is kinda File locking i will give the project specs...guys please help me.. Established client-server architecture using socket... (1 Reply)
Discussion started by: rosemolr
1 Replies

4. Programming

Help Needed Regarding Project

hai guys, I'am a newbie to this forum and to unix as well...as a part of our syllabus i got a project in Unix which i have to do in 4 days.Project is kinda File locking i will give the project specs...guys please help me.. Established client-server architecture using socket... (1 Reply)
Discussion started by: rosemolr
1 Replies

5. Shell Programming and Scripting

Script to monitor Machine Health (Project Doctortux) Input needed.

I have written little script to check the CPU performance of the machine. Request you to contribute your comments on the same. Feel free to add your own scriptlet to make it better. I have decided to call it as doctortux I have decided to run the script in two mode 1)Interactive.(Not... (4 Replies)
Discussion started by: pinga123
4 Replies

6. Shell Programming and Scripting

While Loop Syntax help needed

Hi everyone, Can ny1 help me out regarding while loop arguments i.e. what does -gt -ge -lt -le means? actually i am new to while loops (2 Replies)
Discussion started by: jojo123
2 Replies

7. Programming

Tools for writing a simple syntax checker?

I'm trying to write a small utility for syntax checking. I've tried using Flex/Bison, but these seem too advanced for my task. A simpler tool would be appreciated. (1 Reply)
Discussion started by: Ilja
1 Replies

8. Shell Programming and Scripting

Shell Sript - Spell Checker Assign

Hi Folks. I am currently working on a script that has to spell check a file and print the output to the screen in 2 columns like this. INCORRECT CORRECTION whio who weahter weather The file will allow the user to override the ispell command and save any... (9 Replies)
Discussion started by: ccfc1986
9 Replies

9. Shell Programming and Scripting

project idia in shell

could any one provide me any new real time project idea in shell scripting i am learning shell script and wants to do some real time development . (1 Reply)
Discussion started by: useless79
1 Replies

10. Shell Programming and Scripting

Shell script syntax checker

I have a rather big script that i have written in ksh and it is falling over in two places with a 'test argument' error. I know this usually means that the if statement is not correct, but it is fine. I have looked through the rest of the script for any odd brackets or ` marks, but can't see... (2 Replies)
Discussion started by: handak9
2 Replies
Login or Register to Ask a Question