Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Thursday, September 25, 2008

How do I find the name of the current shell that I am working in

There is a command that will work on all platforms I tested (HP-UX, SunOS, Linux, MacOS) and shells (sh,ksh,bash,csh,zsh)

ps -p $$ | grep $$ | awk '{ print $NF }'

Be aware that the shell name can be bash or -bash

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