IPython is a powerful tool which can significantly speed up and simplify your daily work. Beside the fact that it is interactive, it also allows you to write your script to the file and execute later as many times as you like. In conjunction with shebang, which is a feature of bash, you can turn your script into executable file. For example consider following test.ipy file:

#!/usr/bin/ipython
!echo "Hello world!"

Just change it's attributes by invoking:

chmod +x test.ipy

and you can call your script as any other application by invoking ./test.ipy in bash. Now this is where the magic comes. Imagine that you would like to use IPython in order to extend your git client by adding debug command that would decompress files in ./git/objects directory. This may be a handy command for debugging behavior of git.

What you need to start from is writing following git-debug.ipy file:

import zlib
import os

%cd .git
!rm -fr debug
!mkdir -p debug

%cd objects

dirs = !ls
dirs = [d for d in dirs if len(d)==2]

for d in dirs:
    files = !ls $d
    for f in files:
        print(d + '_' + f)
        with open(d + os.sep + f, 'rb') as fd:
            code = fd.read()
            text = zlib.decompress(code)

            name_raw = '..' + os.sep + 'debug' + os.sep + 'raw'
            with open(name_raw, 'wb') as fd:
                fd.write(text)

            name_txt = '..' + os.sep + 'debug' + os.sep + d + f
            !strings -n 2 $name_raw > $name_txt
            !echo >> $name_txt
            !hexdump -C $name_raw >> $name_txt
            !rm $name_raw

Simple, isn't it? Now change add +x attribute as before and move your script to /usr/bin directory. Note that all the files that start with git- prefix will be interpreted as custom git commands. This means that you can go to your local git repository and test our command by typing:

git debug.ipy

Application should already work as expected. The only thing which may annoy is this .ipy which you must type at the end of the command. To fix this we could rename the script from debug.ipy to debug. The problem however is that IPython wouldn't interpret it as .ipy script any more...

Fortunately there is a tool which can help here. It's called lnipy and can be downloaded here. The idea is simple. You just need to call it giving path to your script as an argument:

lnipy /usr/bin/git-debug.ipy

lnipy will create a symbolic link /usr/bin/git-debug. By calling the link you will indirectly execute IPython with git-debug.ipy as the first argument. All the remaining arguments will be also forwarded as they are. Finally return value of IPython will be returned to the shell. This means that since now, you can simply use your script as any other git command:

git debug

Important thing to note is that if you would like to change the name of the original script, you also need to rename the link accordingly. Only this way IPython will know which script is to be executed by calling the link.