BASH/C2/Array-Operations-in-BASH/English

From Script | Spoken-Tutorial
Revision as of 13:15, 22 August 2013 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Array Operations In BASH

Author: Lavitha Pereira

Keywords: Video tutorial, Bash shell, Array


Visual Cue
Narration
Display Slide 1 Welcome to the spoken tutorial on

Array operations in BASH

Display Slide 2

Learning Objectives

In this tutorial, we will learn how to
  • Declare an Array and assign value to it.
  • Initialize an Array during declaration


Display Slide 3

Learning Objectives (contd.)

* To find length of an Array and its nth element
  • To print an Array


Display Slide 4

Prerequisites


spoken-tutorial.org

To follow this tutorial,

You should be familiar with the Linux Operating System.


If not, for relevant tutorials please visit spoken hyphen tutorial dot org.

Display Slide 5

System Requirements

For this tutorial I am using
  • Ubuntu Linux 12.04 OS and
  • GNU Bash version 4.1.10

GNU Bash version 4 or above is recommended to practise this tutorial.

Display Slide 6

Array Definition

Let us start with the definition of an Array and its characteristics.
  • An Array is a variable with multiple values.
  • The values can be of same or different types


Display Slide 7

Array Charactristics


* There is no maximum limit for the size of an array
  • Array members need not be consecutive
  • Array index always starts with
    zero


Display Slide 8

Declare and assign values to an Array

Syntax:

declare -a arrayname

We will now see how to declare and assign a value to an Array.

The syntax to declare an Array is -

declare space hyphen `a` space arrayname

“declare” keyword is used to declare an Array.

It is a built-in command in Bash.

Display slide 8


Syntax:

Name[index]='value'

The syntax to assign a value to an Array is-

Name opening square bracket index closing square bracket equal to within single quotes value.

=== Display Slide 9 ===

Initialize an Array during declaration

Now let see how to initialize an Array during declaration.
  • Arrays can be declared and initialized at the same time.
  • Each element in the list is separated by a space and is within parentheses.


=== Display Slide 10 ===

Initialize an Array during declaration

Syntax:

declare -a arrayname=('element1' 'element2' 'element3')


The syntax is as shown

declare space hyphen `a` space arrayname equal-to opening round bracket within single quotes 'element1' space 'element2' space

'element3' closing round bracket.

Let us try an example.
Press Ctrl+Alt+T Open the terminal by pressingCtrl+Alt+T keys simultaneously.
On Terminal >>Type gedit array.sh


Type:

gedit space array.sh on the terminal.

Now type the code as shown here into your array.sh file.

Highlight

declare -a Linux=('Debian' 'Redhat' 'Ubuntu' 'Fedora')

This line declares an Array named Linux with elements -
  • Debian,
  • Redhat,
  • Ubuntu and
  • Fedora


Here

hyphen `a` is a flag.

It allows us to read and assign values to an Array.

Let us switch back to the slides.
=== Display Slide ===

Length of an Array and of its nth element.

${#arrayname[@]}

The length of an Array can be obtained by this code:

Dollar symbol opening curly bracket hash arrayname opening square bracket At sign closing square bracket closing curly bracket

=== Display Slide (contd.) ===

${#arrayname[n]}

The length of the nth element can be obtained by this code:

Dollar symbol opening curly bracket hash arrayname opening square bracket index number `n` closing square bracket closing curly bracket.


Here n is the element number, whose length is to be found.

=== Display Slide ===

Print the whole Array

Syntax:

${arrayname[@]}


All the elements of the Array can be printed using this syntax.


Dollar sign opening curlybracket Arrayname opening square bracket `At sign` closing square bracket closing curly bracket.

Come back to the text editor.
[Highlight]

echo -e "Total number elements in an array Linux: ${#Linux[@]} \n"


This line will display total number of elements in the Array Linux.


hyphen `e` enables interpretation of backslash escapes.


We have included this as we have backslash `n` at the end of the line.

echo -e "The elements of array Linux are: ${Linux[@]} \n" Next line displays all the elements of the Array Linux.
echo -e "Third element in array Linux is: ${Linux[2]} \n"


This line displays the 3rd element of the Array Linux.


Please note that an Array always starts with index zero.

echo -e "Length of third element is: ${#Linux[2]} \n" Lastly, this line displays the number of characters present in the 3rd element.
Switch to Terminal>>Type chmod +x array.sh>> Press Enter>>


Switch to the Terminal.

Let's first make the file executable by typingchmod space plus x space array.sh Press Enter.

Type ./array>> Press Enter Now execute by typing

dot slash array.sh

Press Enter.

Output:

[Highlight]

Length of the Array Linux is: 4

The number of elements or the length of the Array `Linux`is four.
Output:

[Highlight]

The elements of Array Linux are: Debian Redhat Ubuntu Fedora

The elements of the Array Linux are Debian, Redhat, Ubuntu, Fedora
Output:

[Highlight]

Third Element in Array Linux is: Ubuntu

The 3rd element of Array Linux is Ubuntu
Output:

[Highlight]

Length of Third Element is: 6

And the number of characters in the third element is six, as expected.


<pause>

Let us summarize. Come back to our slides.
Display slide

Summary

In this tutorial we learnt,
  • Declare and assign values to an Array
  • Initialize an Array during declaration
  • How to find length of an Array and length of its nth element
    and
  • To print whole Array


Display Slide 9

Assignment

As an assignment.

Declare an array names of length 7 and find:

  • the total number of elements
  • print all the elements
  • print the 5th element


Watch the video available at the link shown below

It summarises the Spoken Tutorial project

If you do not have good bandwidth, you can download and watch it

Display Slide


Spoken Tutorial Workshops

The Spoken Tutorial Project Team

Conducts workshops using spoken tutorials

Gives certificates to those who pass an online test

For more details, please write to

contact@spoken-tutorial.org

Display Slide

Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project

It is supported by the National Mission on Education through ICT, MHRD, Government of India

More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro

Display Slide The script has been contributed by FOSSEE and spoken-tutorial team.


This is Ashwini from IIT Bombay.

Thank you for joining.



Contributors and Content Editors

Ashwini