Shell scripts and me
I always feared shell scripts when I was in college. Always knew that it is very powerful if I use it properly, I never did.
Two days back, in I was trying to help one of my college junior to setup Qt4 and the environment to run it. I told him to follow this page. But Pradeepto said the idea of having another user is not so good. and he said:
Apr 30 00:48:21 pradeepto: you can even do make sure that as you changedirectories,
the env-vars are set automagically.
So, I tried to something like that, asked few questions on #bash about how to write a function with same name of a shell command and then call the actual shell command. So, got the shell-builtin-command 'builtin' which executes other shell builtins. Also asked lots of questions to Jace about shell programming :p
My target was, as I change my directory to any directory named 'pyqt4' or sub-directories under it all environment variables should change accordingly to run Qt4 or PyQt4 codes and in other directories the normal environment should stay to run Qt3 :)
The result in my .bashrc :
export tQTDIR="$QTDIR" export tPATH="$PATH" export tMANPATH="$MANPATH" export tLD_LIBRARY_PATH="$LD_LIBRARY_PATH" export tPKG_CONFIG_PATH="$PKG_CONFIG_PATH" export tQTINC="$QTINC" export tQTLIB="$QTLIB" export eFlag=0changePath()
changePath()
{QTDIR="$tQTDIR" PATH="$tPATH" MANPATH="$tMANPATH" LD_LIBRARY_PATH="$tLD_LIBRARY_PATH" PKG_CONFIG_PATH="$tPKG_CONFIG_PATH" QTLIB="$tQTLIB" QTINC="$tQTINC" export QTDIR PATH MANPATH LD_LIBRARY_PATH PKG_CONFIG_PATH QTINC QTLIB } cd () { if [ "$#" -eq "0" ] then if [ "$eFlag" -eq "1" ] then changePath export eFlag=0 fi builtin cd else bname=`echo "$1"|grep pyqt4 ` if [ "${bname}" = "$1" ] then QTDIR=/home/kdedev/src/kde/qt-copy PATH=$QTDIR/bin:$PATH MANPATH=$QTDIR/doc/man:$MANPATH LD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH PKG_CONFIG_PATH=$QTDIR/lib/pkgconfig:$PKG_CONFIG_PATH QTINC=$QTDIR/include QTLIB=$QTDIR/lib export QTDIR PATH MANPATH LD_LIBRARY_PATH PKG_CONFIG_PATH QTINC QTLIB export eFlag=1 builtin cd "$1" else if [ "$1" = ".." ] then builtin cd "$1" dname=`pwd` bname=`echo "$dname"|grep pyqt4 ` if [ "$bname" != "$dname" ] then if [ "$eFlag" -eq "1" ] then changePath export eFlag=0 fi fi else if [ "$eFlag" -eq "1" ] then changePath export eFlag=0 fi builtin cd "$1" fi fi fi }
I think the code is not so much readable in my blog :)
Update: Pradeepto asked me to upload the code as a file, so here it is. Just copy paste the contents of this file into your $HOME/.bashrc .