Category: Ubuntu

How to remove blank lines from text files or php files or js files or…

Posted by – June 22, 2012

By running this command…

egrep --include=*.php -lRZ "^$"  ./ | xargs -0 -l   sed -i -e '/^$/d'

Just two parts to it, search for all *.php files and use sed to replace new lines with nothing.

That’s all.

PyGTK Hello World or: How I learned to write my first GUI application on Ubuntu?

Posted by – September 21, 2010

There are so many things I like about Ubuntu and I can write in extent about all those. My focus is in this post is Python. Python is easy to learn. So easy that one can write GUI apps in just 2 minutes. The title for the post has also indicated this(and is somewhat similar to Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb ). I am a learn by doing person and this post is for same kind of persons. So first the code, with very little or no explanation, shows a hello world widow:

Hello World using Python & PyGTK

Hello World using Python & PyGTK

#!/usr/bin/python

import pygtk
pygtk.require('2.0')
import gtk

class Whc:
	def __init__(self):
		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		self.window.connect("destroy", self.destroy)
		self.window.set_title("Hello world!")
		self.label = gtk.Label("H3ll0 W0rld!")
		self.window.add(self.label)
		self.label.show()
		self.window.show()

	def destroy(self, widget, data=None):
		gtk.main_quit()

	def main(self):
		gtk.main()

if __name__ == "__main__":
	base = Whc()
	base.main()

…and then how I tweaked it

#!/usr/bin/python

import pygtk
pygtk.require('2.0')
import gtk
import os
import commands

class Whc:
	def __init__(self):
		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		self.window.connect("destroy", self.destroy)
		self.window.set_title("Your \"fortune\"")
		self.window.set_default_size(200,100);
		f=commands.getoutput("fortune")
		self.label = gtk.Label(f)
		self.window.add(self.label)
		self.label.show()
		self.window.show()

	def destroy(self, widget, data=None):
		gtk.main_quit()

	def main(self):
		gtk.main()

if __name__ == "__main__":
	base = Whc()
	base.main()

…and then output

Displays a random fortune cookied in GUI

Displays a random fortune cookied in GUI

…and now the analysis. :-)

The code given above uses PyGTK, GTK bindings for Python, GTK, OS and Command module. The OS and Command module is just to fetch the output of fortune command. fortune is a Linux command, more can be found here and may have to be installed, the package is fortune-mod. The Python code is plain English, create a TOPLEVEL window, see for more. In the first code listing another GTK widget Label has been added. The Label contains a snippet of readonly text rendered on Window and in first snippet the text string is legendary, “H3ll0 W0rld!” in L33T. The string in 2nd code snippet is replaced by output of fortune command for which I utilised command and os modules. Easy!!!

This post was lying in drafts for more than 15 months.

Python httplib example

Posted by – August 21, 2010

This is a very simple example of httplib in Python. I am trying to write a very simple client which can simply ping a URL.

import httplib
while True:
 myURL = raw_input('\nURL Please: \n> ')
 if myURL == '':
 print '\URL Please: \n'
 break
httpconnection = httplib.HTTPConnection(myURL)
httpconnection.request('GET', '/')
res = httpconnection.getresponse()
#I am only interested in 200 OK response, anything else can be ignored
if res.status != 200:
 print 'Errr!!!!', myURL, 'seems to be a troublesome URL. The "Internet" says "', res.reason, '" and the status was', res.status, '. Try again.'
else:
 data = res.read()
 allheaders = res.getheaders()
 print data

Ubuntu 9.10 – Karmic Koala

Posted by – November 2, 2009

This is bit dramatic.
Day one, a conversation between me and Dr. Abhishek on phone
Dr.: So whats this with new release of Ubuntu, the Koala…have you heard about it, any idea…?
Me: Yeah, I dont think it will be much different in terms of kernel or any thing related to core, though I do expect changes in UI…
Dr.: hmm…is this LTS?
Me: Nopes, if you are looking for LTS then better wait for six months. I guess I will also wait for LTS. I am too lazy to update my laptops…
Dr.: OK
Day two, on Facebook.
Sukhi’s wall post says, “I am waiting for Karmic Koala” and my comment, “I am not going to install Koala”
Day three, on Facebook.
Sukhi’s wall post is all gaga about Koala.
Last day, Dr. Abhishek’s wall post on Facebook indicates he has installed Koala and he likes it…
I ping him and asked him about this breach and he says he “aiwen hi” downloaded the ISO, created a live version, used it and could not resist installing it.
:-|
Today morning, I have successfully upgraded my Ubuntu 9.04 to Ubuntu 9.10. :-)
Updating or installing Ubuntu is breeze. For a dummy, who can just read English, here are the steps to do it, you will need a 1GB USB stick or a pen drive:
a) Download the latest ISO from http://www.ubuntu.com and save it to C:/ drive or any other place where you have enough space to save a DVD movie. You will be most probably downloading a 32 bit Desktop version.
b) Download Unetbootin for Windows XP or Windows Vist or whichever Windows you are using.
c) Insert your USB/Pen drive in USB and run unetbootin.
d) Choose the ISO option and browse for the ISO file you have downloaded in step a), choose the USB drive which will be in most cases already selected and let unetbootin do rest of the things.
e) As soon as unetbootin finishes it will ask you to reboot or exit. Save your work if you are doing and reboot.
f) In most of the cases your laptop or desktop will boot and show you a screen with options saying something like “Default” and “OEM”. Use arrow keys and select “Default” and hit enter gently with your fingers ;-)
g) In 99.9% cases your computer will show you the familiar Ubuntu logo, in remaining cases you need to contact a geek.
h) Your computer will show you a slick desktop. If you have not disconnected your DSL and other plugins from your computer, you will surprised to know that everything works. Unlike Windows, you dont need to add a device driver or software and reboot your machine.
i) Click Application or press “Alt+F1″ and browse the applications. Check out Open Office, its no where near to MS Office but it will still serve well and its free!
j) If any moment you feel Ubuntu is worth installing, click the install button on your Ubuntu desktop. Installing Ubuntu is very easy but demands a bit of computer knowledge. Let the installer decide what is best for you and 99% chances are that you will have dual boot system in 15-20 minutes. Ready to use.

Did I mention that I was browsing Internet on same laptop at the same time when I was installing Ubuntu 9.04? Can an average Joe do this on Windows?