Saturday, 16 February 2013

Why I love linux system programming?


Happy New Year to all my great readers and may this year bring us a lot of blessings. This is my first blog post for the year 2013 and also writing it a day after my end of semester exams. I am enthusiastic over this one month vacation I have at home , I hope to post a few more articles and see to it that we release a killer app @ Oasiswebsoft ( My software dev company ) before I resume school.

I took a break off application programming in November last year to perfect my skills in linux system programming. The main difference between the two is application programming aims to produce software which provides services to the user (e.g. Anansi Calcpad), whereas systems programming aims to produce software which provides services to the computer hardware (e.g. disk clean up).

Even if you think you're not a programmer, Linux has some features to make your life a bit easier. Any time you have a repetitive task that involves entering Linux commands or changing the contents of a file, you should think about automating it with a program in linux.I installed an old version of Ubuntu 11.10 ( It’s a linux distribution and a stable build ) and started playing around with the terminal ; doing some basic stuff like echoing the name of the terminal , writing simple addition programs , sleep , pwd and spent more time online reading  about file permissions ( Read, Write , Execute ) , appending output file ,variables , shell scripting , process structure , for loops and many interesting stuffs.

The Bash shell is your main port of entry to Linux, since the shell interprets everything you enter on the command line before passing it along to the operating system for execution. I fell in love with shell scripting because unlike java you didn't need to assign data types ( Int , float , string , char ) when declaring a variable and you do not need a terminator (semi – colon ; ) in most instances.
eg. In java :
int $num1 = 70;
in shell scripting :
num1 = 20

Often, writing a shell script is much faster than writing the equivalent code in other programming languages like java and python. The many advantages include easy program or file selection, quick start, and interactive debugging. A shell script can be used to provide sequencing and decision-making linkage around existing programs, and for moderately-sized scripts the absence of a compilation step is an advantage. Interpretive running makes it easy to write debugging code into a script and rerun it to detect and fix bugs. Non-expert users can use scripting to tailor the behavior of programs, and shell scripting provides some limited scope for multiprocessing.
We will write a simple script that will allow us to let the bash sleep for a while,  delete all the files that is starts with KLM.
STEP 1: Let’s create three files in the bash terminal
touch klm1
touch klm2
touch klm3
STEP 2: Let’s give all the three files all  permissions rights for the user, group and others.
Chmod 777 klm*
NB: * stands for all files .
To view all files with the date last edited or permission rights and details use this command ls –la (Long listing) and check if all files have all the rights.
STEP 3:
We will now write a simple program in the VI Editor to sleep for 5 seconds and afterwards delete all files.
echo “ Your text here “
Sleep 5
rm klm*
Final step is to execute this program in the main shell. To execute it use this simple command bash and the name of the file in the VI Editor.eg bash killklm

You have now created a simple shell script that automates the process of putting the shell to sleep and deleting the files that start with the word KLM. With just these few lines of code you can easily build a simple program in Linux.These are screen grubs from the simple script we wrote above in the bash terminal and the VI Editor.




Now let’s write a little complex program. This program that will have 5 options eg.
echo "SELECT A NUMBER“
(1 FOR LOVE         (2 FOR DRUGS  (3 FOR SPORTS       (4 FOR HEALTH
(5 EDUCATION  
Each option will contain a text eg. Love : feel tender affection for somebody so when a user choose is a number 1-5 it will display a text.
In the bash terminal you use the cat syntax to input append a text into a file:
Eg: cat > drugs
You will find the complete source code to a dummy program I wrote here on my github repository it contains a for loop and CASE conditions.  Unlike java I did not have to import any special packages to allow the user to input text. Enjoy!!  https://github.com/raindolf/Options-script 

If you've ever written a simple console program ,word processor macro or a spreadsheet formula, you're a programmer. By taking advantage of Linux's built-in programming features, you can automate repetitive tasks and build simple interactive applications without a degree in computer science.Send me a mail if you have any question or contribution iraindolf [at] gmail.com

"Software is like sex: it's better when it's free." Linus Torvalds

3 comments:

  1. that's good stuff there man, that's a loosely typed language you are playing with. Big ups

    ReplyDelete
  2. Hi, nice description about linux system programming.Thanks for your help..

    -Aparna
    Theosoft

    ReplyDelete
  3. Thanks for review, it was excellent and very informative.
    thank you :)

    ReplyDelete