Tag Archives: script

Easen use of svn switch with bash prompt

The issue

svn switch can safe a lot of time when working in different branches as not all source code has to be checked out again.
But well you know it: How fast does it happen that a file gets committed in the wrong branch?
To prevent you from always have to call svn info before committing, here comes the script which includes the current branch in the bash prompt so you may never commit code in the wrong branch again ;).

Get the script


use Cwd;
my $dir = getcwd;
unless( -r $dir."/.svn" )
$dir=`svn info | grep URL`;
$dir =~ s/n//;
if($dir =~ m//trunk/)
if($dir =~ m/branches/|tags//)
  $dir =~ s/.*(b|t)(ranches|ags)/(.*?)/.*/$1:$3/;
print " [$ret]";

What needs to be installed

Subversion of course and perl to run the script, which is usually installed in most distributions.

How to use it

Let’s store the script to svn-ps1.pl somewhere let’s say $HOME/bin.
Now open the bashrc file with vi ~/.bashrc and adjust the export of the PS1 variable:

PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$ '

to the following:

PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$(~/bin/svn-ps1.pl)$ '

Now simply change to some source code checked out with subversion and you will see that the prompt will look like this:
user@laptop:~/src/svn/some-code [b:branch-name]$


Indent json strings on the bash

The issue

While I was playing around with CouchDB and cURL I pretty quickly missed a feature to indent the json output string. So after looking around in the Internet for a simple solution I hit simplejson which provides methods to indent a json string.

Get the script

With simplejson I’ve created the following python script:

#!/usr/bin/env python

import sys
import simplejson
import string

content = string.join(sys.stdin.readlines())
json = simplejson.loads(content)
print simplejson.dumps(json, sort_keys=True, indent=2)

What needs to be installed

To be able to run the script simplejson needs to be installed. On a debian based system you can easily run following command to assure simplejson is installed:
sudo apt-get install python-simplejson

How to use it

Let’s store the script to jsonindent.py in a executable path directory such as $HOME/bin (run echo $PATH to get more possible directories). To indent a json script returned for instance from a CouchDB you can easily use following command:
curl -sX GET | jsonindent.py

The output will look like this:

  "_id": "hello-world",
  "_rev": "1-97dd85b06c25328a300f3f4041def370",
  "body": "Well hello and welcome to my new blog...",
  "date": "2009/01/15 15:52:20",
  "title": "Hello World"

Looks pretty neat, doesn’t it ;)?