I actually stumbled upon this idea quite a awhile ago along with my friend Sunil. I don’t exactly remember what we were trying to accomplish back then but here is what I got from the little hacking time we had.
This is about how you can mimic commands. You use the commands cd, ls, cp, etc, right? What if you want something of your own like that? For example your name as a command? Ha! Let’s do that itself.
I did this on my Ubuntu 10.04 distro and I have the gstreamer plugins for mp3 files downloaded and installed.
Let’s assume that you have a song called rockthee.mp3 in your /home/<yourname>/music/ directory.
Now what we are going to do is this. We are going to manipulate a few things such that when you type in your name and hit enter, totem movie player will open up and the song rockthee.mp3 will start playing. And we are going to use the system() command to accomplish this.
For this, go to any directory you wish and open up a text editor. Type in the following code there:
system("totem ~/music/rockthee.mp3 &");
Now save it as filename.c and quit the editor.
Going back to the terminal, compile the code as follows:
gcc filename.c -o <your name>
(Replace “<your name>” with your name)
In my case, it would be
gcc filename.c -o haris
Now if you do an ls, you will see that your name is there. Typing ./haris will open up totem and play rockthee.mp3. But that’s not how we want it to be, is it? I mean this is just like executing a C program from the folder it is written in! We shouldn’t use that ‘./’. So what do we do?
Ah ha! We have two options. Either (1) add the path of the current folder (got by executing the command ‘pwd’) to the PATH variable or (2) simply copy the file having your name into any one of the locations given by $PATH.
We will explain both here. But first, try executing ‘$PATH’ in your shell (terminal) and see what comes. Those are the directories which will be searched for when you type in a command at the prompt and hit enter. If you try
you can see many familiar commands including ‘ls’ itself. So when we type ‘ls’ and hit enter, our system searches all the directories specified in the PATH variable and only if it finds ‘ls’ in any one of those locations will ‘ls’ be executed.
So let’s try method (2) first. It is simpler. Run the following:
sudo cp <your name> /bin/
You’re done. Now close the terminal, open up a new one and try simply typing in your name and hitting enter. Viola! There comes Totem with your song! Now to remove what you copied, do
sudo rm /bin/<your name>
Now let’s go to method (1). In order to permanently include the path of your current folder in the PATH variable, you have to edit a file called “.bashrc” in your home folder. But first execute ‘pwd’ and remember the output. Now open up .bashrc using any text editor.
And add the following two lines to the end of that file.
PATH=$PATH:<output of executing ‘pwd’ earlier>
In my case it would be
PATH=$PATH:/home/haris/music/ (assuming I wrote and compiled filename.c in the ‘music’ folder itself)
Now save the file and close it. Close your current shell (terminal) and open a new one. Type in your name and hit enter and see the wonder works! Also now try executing ‘$PATH’ and you will see that a new path has been added to it.
You can make it a bit more interesting if you add a few printf statements in that C program before the system command saying “This software is created by haris and its version is 0.xx” or something. You can show your non-linux user friends and mess with them.
You can try system command for many things. Here are a few examples:
system(“firefox gmail.com &”);
system(“evince <path>/filename.pdf &”);
system(“gedit <path>/filename.c &”);
Try playing with it and I’m sure you will get more and more ideas.
All the very best!