Monday, January 14, 2008

Useful BASH string manipulation tricks

A lot of cool bash string manipulation http://www.linuxjournal.com/article/8919

error: cout is not a member of std

What the ....?

Q:What is wrong with this block of code in a program?

std::cout << "HelloWorld\n";


A:
#include <iostream>

Wednesday, January 9, 2008

Zombies

Zombies!! What are they?
Zombies are walking corpses, they sometimes talk but in general they are not very talkative. Zombies being not very talkative is the main problems in dealing with them. However, we can always find them and deal with them. The easiest way to deal with them is to restart, which is not very good in a lot of times.

Zombies
are those children created by a parent process whose parent died before reaping them

In other words, some lousy piece of code written by some lousy programmer that forks children. However, the programmer didn't account for the possibility that the parent might die before its children. The net result of this mess is a bunch of zombies leeching system resources and leaving your machine unsure of the next step.
Reasons to create zombies:
  1. The programmer was really very bored and didn't want to check
  2. The assumption that forked children will be nice and die before their parents
  3. The assumption that the parent will be mean and not die unless kills all children
  4. The assumption that someone will come and kill those children, which actually happens when the init process takes over and clean up things, but this is no execuse
  5. The programmer was out of coffee or time
  6. The programmer needed more time and a more screwed up machine to test his screwed up code
  7. The machine is actually a zombie, so the programmer won't notice more zombies
  8. Zombie infection might have taken over the machine in an unrecoverable way, through open ports. It'll get worse when the infection is done by a tunnel and even worse when the machine doest not have a good antibiotic or vaccinations
Solutions to the above mentioned problems:
  1. The programmer should never feel bored and should keep coding till one of two things happen:
    1. Power outage
    2. Coffee leak
  2. Children are never nice and won't die by themselves. They must be killed and reaped by their parent in a very brutal and gory way
  3. Parents will never bother to reap their children automatically because they are really easy and have the tendency to screw the programmer. The programmer should threaten the parents by issuing random SIG_INT so they'll be afraid of leaving their children alone!!
  4. No one will come and clean zombies processes from your system!! Go wait (kill) them by yourself
  5. Programmer out of coffee is a FATAL error and nothing much can be done about it, responsibility is put on the supervisor! However as for time, the programmer should never relay on the accuracy of time as this electron around the Cesium is not really what you mean by accurate
  6. Time again (please refer to 5). A screwed up machine is very easy (use VISTA)
  7. Programmer is completely responsible for turning the machine into a zombie. The programmer should leave the machine to his/her younger brother or sister if any. If not the programmer should be careful and turn this zombies machine into a coffee machine and proceed to the nearest computer store and to buy some new gear. Things to remember when buying new gear is how easily the new gear is transformable to a coffee machine or cubs holder
  8. Whenever a zombies infection is present on the machine go SLEEP and don't go out under sunlight... it is you the programmer who turned to a zombie
In any event, I was really bored and I wrote this silly shell function to tell me if there are zombies and tried to kill them. Please note that mechanism of killing zombies are beyond the scope of this post. Also, comments are left as an exercise for the reader..

zombies() {
z=`ps aux | awk '{ print $8 " " $2 }' | grep -w Z`
id=${z#Z}
if [ ! -z "$z" ]; then
  echo -e "You have a zombie `whoami`"
  echo "ID: $z"
  ps aux | grep Z
  echo "Killing $id"
  kill -9 $id
else
  echo "No Zombies!!"
fi
}


Ok world, we'll get back to more interesting things like daemons very very soon.. stay tuned :)

Saturday, January 5, 2008

Colors...Grrrrrrrr [frawn]

Hey World...
I have no clue why on earth my choice of colors is really really silly....
i don't even like them.. and I can't find better colors..
Whenever i try to fix it..I just make it worse.. Grrrrrrrrrrr..

I'll get back to fixing colors later..

i hate colors....

Friday, January 4, 2008

Static IP address in Linux

There are many occasions in which you might like to have a static ip address for you machine instead of the one generated dynamically by the router.. Maybe you want to run a webserver, or ftp server, or maybe want to SSH to your machine from any place.. Then you need to now the IP address of the machine, which is not very easy if you are not in the local network... i'm bored of reasons.. let's get to the point:

To assign ur machine a static ip address do this:

Edit /etc/network/interfaces
to have this:

auto eth0
#iface eth0 inet dhcp
iface eth0 inet static
address 192.168.1.10
network 192.168.1.0
netmask 255.255.255.0
gateway 192.168.1.1
broadcast 192.168.1.255

Then, restart networking

sudo /etc/init.d/networking restart

And check the new address by

ifconfig eth0 | grep "inet addr"


Done!!

Testing Code CSS

Finally, let us use CSS to post code, to save time and make it look good
This is only a test for the new style sheet.
-----
The style is subject to change without warning
-----


#include<stdio.h>
int main()
{
printf("HelloWorld!\n");
return 0;
}


Does that look good?
Hmm.... Good enough :)

Change the Default Gnome Terminal size

Gnome terminal comes really small (80x24) which is really small. Further, the GUI provided with it does not let you change the default size easily. So, you have two options:
  1. Keep changing the size manually with the mouse every time you launch a terminal (because Gnome terminal doesn't save its size)
  2. Look for those configuration file and change them
We'll do option 2, apparently option 1 is silly, boring and counter productive.

Gnome terminal follows termcap specifications, so you should find your xterm termcap file and edit it. (We'll get back to what a termcap is in some other posts)

The file in Ubuntu and most other distros is found in
/usr/share/vte/termcap
you'll find this in the file:
:co#80:it#8:li#24:\
change whatever after co# and li# to your desired settings
for me i love this one
:co#90:it#8:li#26:\

And restart you machine..
Done!!!

Other possibilities are also available..
One thing you can do is to right click on the Gnome Terminal launcher in your Gnome Panel and chose properties, which look like something like this:
Then add this next to "Command" --geometry 90x26, so you'll get this:
And this will work also...
At the end of the day, you have permanently changed the Gnome terminal size :)

... Done!!!

Restore your linux

Well.. let's go format Windows... thanks to the nice virus, but this will erase my ubuntu from MBR and I'll not be able to boot into it..
So, after installing windows and wiping out the windows partition. You get the ubuntu live CD and do the following:
sudo grub
find /boot/grub/stage1
This will tell you where are the boot files, something like hd0 or sda1, so you use that in the next step
root (hda0, 1)
setup (hda0)
quit
Remove the liveCD && restart your machine

Now you have a fully functioning Linux partition, if you haven't wiped it out accidentally while reinstalling windows.

More stuff in here:
http://ubuntuforums.org/showthread.php?t=224351

Thursday, January 3, 2008

Awful..!!!

Sorry world for the awful colors in this post :( http://byeworld.blogspot.com/2007/12/bash-loops.html

Just an artifact from the 2007 :P