Sunday, April 18, 2010

Mono, C Sharp and Shell scripts

Greetings to all!

This being my first step into blogosphere, I would like to share something novel, or Novell should I say... This will describe how to create a simple application in Mono, an open implementation of the .NET framework for Linux, built by Novell. This post will also show how to call a shell script from a mono application.


Ubuntu 9.10 - I had heard that it has one of the most user-friendly interfaces - and so it turned out - even the terminal console was white by default - nice...

Here's something about mono.

Most newbies can't code without an IDE (Integrated Development Environment) to do all the dirty work - so, I got Monodevelop, an opensource IDE for C# development in Mono, on Gnome-based versions of Linux.

(A little experience here - Mono doesn't work very well with KDE-based Linux implementations - as I came to know from my travails with Knoppix and Kubuntu).

Getting Mono and MonoDevelop

To install Mono and Monodevelop on Ubuntu 9.10 (I was unsuccessful in installing them for other versions of Linux):
Open a terminal and type this (ensure you are connected to the Internet on Linux):

sudo apt-get install monodevelop

This is how your terminal should look as it happens...

This will download and install Monodevelop onto your Linux machine/ Virtual Machine.
Then, you get a 'Programming' option from your 'Applications' menu - and it has Monodevelop as the only option. Click and you are now using monodevelop!

Using Monodevelop


The IDE looks familiar - all those who have used Visual Studio might agree with me on that one...

But here comes the nice part - you can create GUI applications using Mono for Linux...

The application in the screenshot is a contacts manager - I dragged and dropped a 'fixed container' onto the form of new Gtk# forms application... (I am guessing that is the equivalent of Winforms for Linux Gnome). Then I added a few buttons, labels and began writing event handlers in C#. You do that by selecting the button, choosing the 'signals' property from the Properties window and choosing the 'clicked' event.

Okay, so many of you already know about Mono and C# - but any amazing application is not just a snazzy GUI, but also a robust back-end...

So I thought, what would be a more robust backend than a shell script?

Mono and shell scripts

Of course, I can use C# and MySQL, but me being the newbie I am - decided to stick what I had been taught in training at work - hard, unattractive but very effective shell scripting.

So here's some C# code I used in a button event to call a bash shell script to save (write) contact details to a file...

String args = entry1.text+" "+entry2.text+" "+entry3.text;
System.Diagnostics.Process p= System.Diagnostics.Process.Start("sh fileSaveScript.sh", args);
p.WaitForExit();

The 'entry' is the Monodevelop equivalent for 'TextBox' in Visual Studio.

And here's the shell script for completeness' sake:

# $1 = Name
# $2 = Phone number
# $3 = Address

# file saving logic:
name=$1
phone=$2
address=$3
echo "Saving contact to file Contacts.txt"

if [ -n $name -a -n $phone -a -n $address ]
then
echo $name $phone $address > Contacts.txt
fi

And when I run the .exe, voila! It takes the details from the text box and writes them to the file.

I can also add a similar script to retrieve data from the same file... all in all the thought of running shell scripts with GUI made me feel great...

So these were all the 'know-gets' I have to share for the moment - until my next foray...

Hasta la vista!

2 comments:

ACKs - Response-driven readers who reply to packets thrown at them