Friday, March 6, 2009

Introduction to Unix Shell Scripting

I would like to discuss Shell Scripting in Unix in this topic today. What I would be discussing with you is introduction to Shell Scripting, its features, command line parameters, arithmetic operations, control structures and functions usage in Shell Scripting.

Lets start with the definitions. Shell is a program provided by Unix, typically to provide interface to the Unix system. Thus, in other words, Shell is some tool for interacting user with system. Generally its either text based and command line oriented. Command Interpreter which is series of commands and takes commands from user and executes them. There are different types of Shell e..g Bourne Shell (/bin/sh), C Shell (/bin/csh), TC Shell (/bin/tcsh), Korn Shell (/bin/ksh), Bash (Bourne Again shell) (/bin/ksh) etc.

Why do we need Shell Programs ? Nice question you have... Lets concentrate now on this basic question which is generally asked. We generally may need to run tasks that are customized for different system or Some times we need to write some jobs that run on a system and we need to control those jobs we may need Shell Scripting in such and situations like those.

1.Script starts with line #!/bin/sh
2.'#' is used for commenting in script. Comments used in script always makes your code readable
3.It gives identification of author of script and date & time of update.
4.Easy to read and understand
5.Major complex sections of codes can be explained
6.Last but not least Versioning is tracked through this

These are few of features that we can generally think of it.

Lets see now what are the parameters and its usage through command line.

$0 tells you the name of script. $1, $2, $3 … tells you the 1st , 2nd , 3rd and so on parameter passed through the command line. And $# gives you no. of arguments passed to the script. If your script needs to read the input given by user through command prompt, use “read” statement .
e.g. echo “Please enter your name : ”
read name
where name would be the variable holding the user inputted value.

How arithmetic operations are carried out in Scripting is still a question. Let me explain you through the example.
e.g. i=2
j=3
k=`expr $j - $i`
echo “Result of $j - $i is : $k ”
Note here, expr is used to execute the operation and it would assign the result to the variable on left side.

Just like other programming languages, Shell Scripting also provide couple of control structures for your help. Significance of control structures is well knows world wide.. :)
If – then, For loop, While loop, Case statement are couple of control structures provided by Shell Scripting.

Lets see Syntaxes one by one for all Control Structures :
1.If- then
Syntax:
if condition then
condition is satisfied and is true
execute this block
else
if condition is not satisfied then execute this block upto fi.
fi

Here, note that condition is zero means its true and satisfied. And you can go in nested
if -else – fi .

2.For Loop
Syntax:
for { variable name } in { list }
do
execute and repeat all the statements in this block once for each item till
the list is finished
done

3.While Loop
Syntax:
while [ condition ]
do
command1
command2
command3
..
....
done
Here, Loop is executed as long as given condition is true. You need to make sure, at some point condition should get false within the loop or else it would go on executing indefinitely causing script hang.

4.Case Statement
The case statement is good alternative to Multilevel if-then-else-fi statement. It enable you to match several values against one variable. Its easier to read and write.
Syntax:
case $varName in
pattern1) command1
command2
…..
commandn;;
pattern2) command1
command2
…..
commandn;;
.
.
.
.
patternN) command1
command2
…..
commandn;;
*) command;;
esac

The $varName is compared with patterns till a match is found. And similar to case statements in
other programming languages such as PHP, shell executes all the statements up to the two semicolons as a delimiter.. *) is the default case and executed when no pattern is matched.

Hope at this point of line, you have been very much comfortable with Shell Scripting and its basics. Finally lets see how user defined functions work in Shell Scripting.

In Shell Scripting Function can be defined as a series of commands. They performs specific work to do or simply say task. Below mentioned syntax can help you understand the function in detail.
Syntax:
function_name ( )
{
command1
command2
.
.
.
.
commandN
return
}
Here, function_name is the name of function that you define, and it executes series of commands either up to return statement or end curly brace of function (whichever encounters first ) which terminates the function and passes control back to the calling point
e.g.

Type fnGetName() at $ prompt as follows
fnGetName()
{
echo “in function”
}
Call to the above function can be given with simply name of fucntion.
e.g. fnGetName

fuew !! Tired so far reading ?? ha ha .. we are not done yet but for now.. yes..!! This is enough for you to get basic idea of what is Shell Scripting. We still have tons of other areas in Shell Scripting to cover which is vast in size and will need separate site to explain and discuss it. Lets discuss it further later at some point, in mean while you can always get in touch with me for your queries through this blog and will be happy to resolve them.

Till then aloha !!

2 comments:

  1. Hi Sandip,

    Thanks for this valuable information...

    I am a bit eager to know if you are planning to give some ideas on how to create shell script, how to execute it and how, where and why to use it?

    Thanks in advance!!!

    ReplyDelete
  2. Hi Prashant,

    Not sure if you checked, but herez something more I have put in.
    http://sandip-tekale.blogspot.com/2010/02/shell-scripting-continued.html

    Hope it should help you... Let me know if any question !! Wish you Happy Programming...

    ReplyDelete