Archive for November, 2009

Implementing Yes/No to continue

November 18, 2009 Leave a comment

Here is a quick how-to for doing something like “Do you wish to continue Yes/No” within shell scripting:

 echo "Will you like to delete $dir under $PWD for a fresh extract? (y/n)"
 read opt
        case $opt in
                Y|y) cleanfolder $dir ;;
                [Yy][Ee][Ss]) cleanfolder $dir ;;
                N|n) exit ;;
                [Nn][Oo]) exit ;;
                *) exit;;

This will look for almost all kinds of combination of Yes/No options and do the appropriate action depending on the user input.

Shoaib Mir

Option parser in Python

November 18, 2009 Leave a comment

I have just started playing around with Python like a few weeks back and having lots of fun scripting in it. Today at work got to know of a very nice feature with Python which lets you easily configure the options taken from command line, which is called option parser.

Firstly, you need to include the library with ‘import’:

from optparse import OptionParser

Now you can use it like this:

usage = "usage: %prog -H hostname -p port -d diskpath -t threshold"
parser = OptionParser(usage=usage, version="%prog 1.0")
parser.add_option("-H", "--host", action="store", type="string", dest="hostname",
help="input host name for database server")
parser.add_option("-d", "--disk", action="store",
type="string", dest="diskpath", help="input disk path")
parser.add_option("-p", "--port", action="store", type="string",
dest="dbport", help="input port for database server")
parser.add_option("-t", "--thresh", action="store", type="int",
dest="thresh", help="input threshold for disk size ")

Where every add_option means adding a new argument on command line to check for and the best thing is you just put in the help section with the add_option function call and it automagically builds the full help section for the program to be use with “-h”, so that means you cant have an option with “-h” as that clashes with the default help option for option parser, and that was the reason I had to go with “-H” for hostname 🙂

In order to get the values into variables just simply use:

(options, args) = parser.parse_args()
if options.hostname:
if options.diskpath:
if options.dbport:
if options.thresh:

Now when you run the program with “-h” option you get the following:

shoaib@shoaib-desktop:~/Desktop/Scripts$ ./mgmnt -h
Usage: mgmnt -H hostname -p port -d diskpath -t threshold
--version             show program's version number and exit
-h, --help            show this help message and exit
input host name for database server
input disk path
-p DBPORT, --port=DBPORT
input port for database server
-t THRESH, --thresh=THRESH
input threshold for disk size

Shoaib Mir

Categories: Python Tags: ,

GCC tricks for the Linux Kernel

November 3, 2009 Leave a comment

While debugging some GCC specific linking problems, came across this very handy resource for gcc tips and tricks… very helpful indeed!

Check it out at –>

Shoaib Mir

Categories: C/C++ Tags: , ,

Disable notification system in Ubuntu

November 3, 2009 1 comment

I dont know about everyone else but for some reason I just found the notification system really annoying with my Ubuntu setup, after struggling with a few files and settings finally was able to get it all disabled.

Here is a quick how-to in case someone wants to disable/enable it….

For disabling use the following:

sudo mv /usr/share/dbus-1/services/org.freedesktop.Notifications.service 

To re-enable it:

sudo mv /usr/share/dbus-1/services/org.freedesktop.Notifications.service.
disabled /usr/share/dbus-1/services/org.freedesktop.Notifications.service

Shoaib Mir

Scripting within psql

November 2, 2009 2 comments

In one of my previous posts I was told of using PSQL instead of shell scripting to make things much simpler. That turned out to a good tip and with some help from my colleague at work I am now so used to PSQL in these kind of scenarios.

Just today I had to get the dead tuple percentages (using pgstattuple contrib) for tables within all databases on the server and here is how I was to get it done using PSQL and a simple shell script….

PSQL="psql -A -t"
DBLIST=$($PSQL postgres -c "select datname from pg_database
where not datistemplate and datname not in ('postgres')")
for DB in $DBLIST; do
 echo $DB;
 psql $DB  < gettupd.sql > $DB.out
exit 0


\o /tmp/tupd.sql
select 'SELECT \'' || relname || '\', * from pgstattuple('''||schemaname||
'.'||relname||''');' from pg_stat_user_tables ;
\i /tmp/tupd.sql
\! rm /tmp/tupd.sql

PSQL is just the way to go…  🙂

Shoaib Mir