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  :) 

No comments:

Post a Comment