Friday, May 30, 2008

Script Shell Syntax checking

I'm writing a lot of stuff in shell (Korn,Bourne,Bash) and I had a need for syntax checker in the build time. Something I could use either in the command line (like a compiler) or in the make time which should stop the build whenever there is a syntax error in my script.

Here is what I came up to. Simply copy/paste the code below to a file named shck.sh (or whatever you wish to call it), do chmod+x shck.sh and off you go!

Usage:
shck.sh myscript.sh
-or-
cat myscript.sh | shck.sh

You can also use it in the makefile. It will return value 1 if there was an error.
-----------------------------------------------------
shck_return() {
    return $1
}
shck_errno=0
echo "set -n ">random.$$.sh
cat $1 >>random.$$.sh
chmod +x random.$$.sh
./random.$$.sh >errors.$$.err 2>errors.$$.err
rm -f random.$$.sh
grep -v ": warning: line" errors.$$.err | sed -e s/random.$$.sh/$1/g >errors.$$.err2
[ -s errors.$$.err2 ] && {
    shck_errno=1
    cat errors.$$.err2
}
rm -f errors.$$.err errors.$$.err2
shck_return $shck_errno

No comments: