Archive for February, 2009

Create and access shell variable having a name created by another string

February 26, 2009

This took sometime to find. Apparently this was not something possible with any direct way. I wanted to have 2 dimentional array but in the end i decided to use dynamic strings to create variables which would simulate the 2d array.

Say i want n variables like “test1,test2,test3 …… testn” where n is decided at the time the script it running. So what I should do to create the variable is pretty simple. I will show you how to create the nth variable.


$ n=123
$ let test$n=$n*2

at this point there is a variable now ‘test123’ and it have the value 246. to get the value is a little tricky. This is what took me a lot of time to find. In the end i found the solution in

First you have to create a variable which contains our dynamic variable name.

$ varName=test$n

Now the variable “varName” contains our  dynamic variable name. If you print the value of this variable

$ echo ${varName}

but if you specify “!” mark infront of our variable then the value denoted by the variable whos name is the value of “varName” is shown.

$ echo ${!varName}

well thats it actually. Hope this saved some time for someone.


How to save the output of a shell command to a variable

February 25, 2009

This is fairly easy to do compared to the amount of trouble i had to go through to find it. All you have to do is to surround the shell command with the “`” character (this is not the single quote character) and assign the whole string to the variable name.

$ n=`ls`
$ echo $n
pom.xml src target

Its as simple as that. Have fun.

How to findout what ports are free using a linux shell script

February 25, 2009

Well it seems there is no one single command to do this. But what we can find out is already open ports. Using this we can find free ports by looping through port numbers.

Well the command is this:

$ lsof -iTCP:32000

The above command will search for the open port 32000 running the TCP protocol. If the port 32000 is open then there will be an output with the relevant details, else there will be no output. To use this information programatically we can do the following.

n=`lsof -iTCP:32000`

if [ "$n" != "" ]
    echo "Port is being used"
    echo "Port is free"

The variable takes the output and if it is a empty string then the port is free else it is being used. (might be a better way of doing this check). All you have to do is to wrap this code around a loop to test the required port.

Include a shell script file in to another schell script like in #include in C

February 24, 2009

This took a little time to find. But apparently this is very easy. I wanted to share some data in a shell script among some other shell script. So the obvious this to do is to include that shell script inside other shell scripts. To do this we have to use the “source” command.

echo "This is my script"
echo "Called the other script"

What source command does is just execute the shell script and sharing the execution environment of the currently executing script. If you just call the script as ./ it will just execute with its own stack.

Monawada Muththe Mokada Karanne/මොනවද මුත්තේ මොකද කරන්නේ – අනාගත දායාදය

February 24, 2009

All sri lankans who lived for the past 20-30 years will remember this song. I was riding in bus to office today morning and all of a sudden i remembered this song for some reason. When I was a kid I used to hear this all the time and each time I listened to it with alot of enthusiasm. And I was wondering why I no longer hear this song on TV. Pretty sad actually given that this song gives a very important message or rather advise to the community. So I came to office and first thing I did was to search this on the net. And there it was :).

So I hope everyone will enjoy this song as I have.

Here are the lyrics…

අනාගත දායාදය…

මොනවද මුත්තේ මොකද කරන්නේ

මොකටද ඔබ ඔය බිම හාරන්නේ..

ඔබටත් ළමයෝ නොපෙනේ නොසිතමි

අඹ ඇටයක් සිටුවන්නට හාරමි. //


දුකසේ සිටුවා අඹ ඇටයක් අද

එක අඹයක්වත් කන්න ලැබේවිද

මම ළමයෝ මින් අඹයක් නොපතමි

යුතුකම පමණක් ඉටුකොට තබනෙමි

තමන්ට නොලැබෙන දේකින් ලෝකෙට

කරන්ට හැකි දේ නොම තේරෙයි මට

පුදුමයි ළමයෝ ඔබේ ඔය අදහස

හැදුනොත් ඔහොමට ඔබටම වෙයි දොස

පෙර උන් අය සිටවූ අඹ ගස්වල

ඵලයෙන් අප කවුරුත් ලබනෙමු ඵල

අපෙන් පසුව එන අයටත් එම ඵල

ලැබෙන්ට සැලසුම අපගේ යුතුකම

ඒ යුතුකම හොඳ හැටි දැනගත්තෙමි

ආයුබොවන් මුත්තේ මම යන්නෙමි //

Little info on the song:

This is Rohana Weerasinghe & Anusha Nanayakkara
Lyrics from Ven. S. Mahinda (Tibetan)
Music : Rohana Weerasinghe