Saturday, February 9, 2013

Basic of Shell Programming

Why shell?
Even though there are various graphical interfaces available for Linux, the shell still is a very neat tool. The shell is not just a collection of commands but really good programming language. You can automate a lot of tasks with it. The shell is very good for system administrator tasks. You can quickly try out if your idea which makes it very useful for simple prototyping and it is very useful for small utilities that perform some relatively simple tasks where efficiency is less important than ease of configuration, maintenance and portability.
Let's see how it works:
Creating a script file:-
There are two ways to create a script file, i.e. we can save script file with .sh extension that will indicate that it's a shell file or we can write “#!/bin/sh” at the very first line. The “#!” character tells the system that the first argument that follows on the line is the program to be used to execute this file. In this case “/bin/sh” is shell we use. And for creating script file we can used any text editor like GEDIT, VI editor etc.
Variables:-
As we know every programming language needs variables, without variables it’s next to impossible to do programs. But in shell programming we don't have data types. All variables are of string data types and we don't need to declare it, to assign values to variables. Syntax is as follows:-
Variable_name=value
Even we don't need terminator operator to end statement, actually its optional, you can used it if you want.
Examples:-
str=”This is shell Programming ”
num=169
You can also print the values of variables by just using “$” symbol in front of variables.
echo “values of num is=$num”
It will print value of num that is 169 on the screen.
Comments:-
In Shell programming we can use comments for user understanding using # symbol at first of your statements.
Example:-
#This is my first shell program.
#Program based on Linux.
Reading values from Keyboard:-
This is very important to take values from user, as I mention above shell programming doesn't have data type so we don't need to specify which type of values we need to read. To read values from keyboard just use “read” command as given below:-
echo "Enter value for A"
read A
echo "Enter value for B"
read B
C=`expr $A + $B` - Line5
echo "Addtion is =$C"
Now, here some more things come at Line5 that is back quotes and expression (expr). To do any kind of calculation we have to follow this procedure.
In shell programming we have many things to do, but above things are the basic which we have to know at the initial stage of shell programming. For more information you can search it on www.google.com or you can also mail me on panditdilipr@gmail.com .

No comments:

Post a Comment