Wednesday, September 25, 2013

Shell Programming 11 : Arrays

Hello ... Friends today we are going to discus about Arrays in the shell scripting. like other programming language Arrays also having an important role in shell too.
so basically in shell the array is only one dimensional. & Ist element start with value [0] , not with [1] , it follows the same rule as C Language.

How to assign Array : there are mainly thee ways of assigning Array in shell
1. one at a time
2. all at once
3. by index

so now let's understand how to assign them in given ways
one at a Time : by this method we can assign array & values like as we define variables one at a time.

like :

numberarray [0]=zero               
numberarray [1]=one
numberarray [2]=two
numberarray [3]=three

we can also define it as

numberarray [0]=zero               
numberarray [1]=one
numberarray [3]=three

here we miss the value or array numberarray [2]=two 
& we also can use space in values 
if we have two part of values then we can add space too but need quote like

country  [0]= India
country  [1]= "United Kingdom"
country  [2]= Canada
country  [3]= "United State of America"

this is one dimensional Array

All at Once : we can also assign arrays in a single list by just separating values by space
team = ( Rabbit DeadMan Parth IHacker NullPort )

 as we can see it is much more different then "One at a time"  assign array  we have some limitations in All at Once array like :

1. We do not use spaces between them if we have space then it will assign for other array
2. we have to know the value of arrays too

By Index :

By Index type array we need to provide the name of the variable every time.

example : nonprinting= ( [0]=NULL  [1]=SOH   [2]=STX   [3]=ETX  [4]=EOT  ..........[15]=US)  this is by Index array type.

thnx friends , Hope you like it & i will try to make it so easy that every one can understand this all & tiring to increase the complexity slowly . Thnx again  :) 

Friday, September 6, 2013

Shell Programming : 10 | Functions

Hello.... Friends Today we are going to discus about the functions in the Shell Programming  or shell Scripting. as we knows a function have vary important role in any Programming Language. Basically we use the functions to define any specific set of code & that set of code which is defined under functions can be called at another part of programs as we want.
so we can say that any function is a way of arrangements of code in a small part & well manners.

Let's check how to define a function in a shell programming...

How to Define a function :

Basically in shell script function is define by pair of parentheses () or by curly brackets {} just after function name.

for exp.

this is a very simple Demo to define a function here func() is our function & the function is enclosed in curly brackets {}  this is the condition we assign, here one this to be understand is if i want the same output at other part of program then now i have to just call the func() now we not need to write the program inside curly brackets {} again & again.

To understand func. let's take more example:
this is our vary simple script which gives us the useful information about our system by using function call, here i am just calling a function mach() & in function i define the various commands
for Machine information and calling this function with mach at the end.


here is the output file which we collect from our above script :)


In this tutorial we just need to understand what is function, how to call it & how to use it in scripts .
thank you  ~:) ~:)

Monday, September 2, 2013

Shell Programming : 9 | Using Loops

Hello.... Friends Today we are going to discus about the looping in the shell scripting loops are basically block of code that is executed until the condition is meeting our need. 
So basically there are four types of loops in shell programming :
  • for
  • while
  • until
  • select
these all have there own functions & there purpose and having there own powers.
So let's start one by one 
  • for loop 
for loop is a loop which does not test the condition variable again & again each time . it just start with the list of items from start to end & test up to when get the result.so for loop is best when you know that what to do or the list is given, the for loop having the do & Done condition. it start with do & finish with done   
for example :
if we want to know the value from a given list then we can use it as :


In this script first we are asking from the user for the list & then as the list is given the for loop start & stop after done
  • while 
while loop as it's name "while" until the condition is true or we can say while loop is a loop which is executed till the given condition is true.
 let we clear it with an example : as if we want to find the multiple of 2 from 1-100, then how we write the script for this, that means all the true condition is displayed the result will be as ,2,4,8,16,32,64, now we know 128 is out of range or out of condition. then lets write a script for this.


while loop is mostly used when no any list is given to us like in for loop we have a list but we have just a condition to test. 
  •  until 
until loop is like as while loop but the difference is only there is no test conditions like while loop so lets understand it more clearly with a script.

here in this script we ask to enter the starting  values as a & b. & we set a until loop here that the string of a is not greater than 15  & string not b less than 200 . this is our loop condition then as we put the value of a is less than 15 is 5 and value of b is greater than 200 is same as 200. then the loop just start & end at only single string.
as we give starting value of a is 10 then loop run up to 15 at one hand & if we put starting value 250 then as our next condition a=$a+1 & b=$b-10 also followed & we obtain the result as soon in the figures.
  • select 
the select loop is nothing but just a loop simply which define the values from a list that what about that value you selected we will discus select with more advance options later , here i am just giving a simple example a list & we have to select one by Number & see what is it's response.



please leave your comments & likes & follow my blog to learn more ...