BASH/C2/More-on-Arrays/English

From Script | Spoken-Tutorial
Revision as of 11:14, 26 August 2013 by Ashwini (Talk | contribs)

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

Title of script: More on Arrays in BASH Shell Scripting

Author: Lavitha Pereira

Keywords: video tutorial, Bash shell, Array


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

More on Arrays in BASH

Display Slide 2

Learning Objectives

In this tutorial, we will learn to
  • Extract an element from an Array
  • Replace an element in an Array
  • Add an element to an Array
  • Remove an element from an Array


Display Slide 4

Prerequisite


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 practice this tutorial.

Display Slide 6

“Extract of element from an Array”


* The elements in an Array can be extracted from any position
  • Here, position is the index number.
  • Note that index number always starts from zero


Display Slide 6

Syntax:


ArrayName[@]:position:Number of elements

The syntax is as follows:

ArrayName opening square bracket At sign closing square bracket colon position colon Number of elements to be extracted from the position mentioned.

Let us understand with the help of an example.
Press Ctrl+Alt+T Open the terminal by pressingCtrl+Alt+T keys simultaneously
On Terminal >>Type gedit array2.sh Type:

gedit space array2.sh on the terminal.

Now type the code as shown here in your array2.sh file.
[Highlight]

#!/bin/bash

This is the Shenbang line
[Highlight]

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

This declare command declares an array named Linux with elements
  • Debian,
  • Redhat,
  • Ubuntu and
  • Fedora


echo -e "Original elements in an array Linux: ${Linux[@]} \n" This echo command will print the list of all the elements in the array.
echo -e "The two elements starting from index one(Redhat): ${Linux[@]:1:2}\n"


The next echo command will print the extracted elements.


The command

${Linux[@]:1:2}

will print two elements starting from index one which is Redhat.

Switch to Terminal>>Type chmod +x array2.sh>> Press Enter Switch to Terminal.

First let's make the file executable by typingchmod space plus x space array2.sh .Press Enter.

Type ./array2.sh>> Press Enter Now let's execute it by typing

dot slash array2.sh

Press Enter.

OUTPUT


[Highlight]

Original elements in an array Linux: Debian Redhat Ubuntu Fedora


We get the output as shown -


Original elements in the array Linux: Debian Redhat Ubuntu Fedora



OUTPUT


[Highlight]

The two elements starting from index one(Redhat): Redhat Ubuntu


The two elements starting from index one(Redhat): Redhat Ubuntu
Let us switch back to the slides
===== Display Slide 7 =====
“Replace an element in an Array”

ArrayName[n]='NewWord'


An existing element in an Array can be replaced using the following syntax.ArrayName opening square bracket n closing square bracket equal to within single quote, NewWord.


Here 'n' is the index number or element number.

Come back to the text editor.
[Highlight]

Linux[2]='Mandriva'

Linux[2]='Mandriva'

This command will replace the third element Ubuntu with Mandriva.

echo -e "All elements after replacement: ${Linux[@]} \n" This echo command will display all elements of array Linux after replacement.Till here will execute and show.
I did not understand this line here.Reply to Nancy Varkey (08/08/2013, 11:13): "..."

It says, as the script was already executed, we will not execute again, instead we will browse through its outputAs array2.sh is already executed.

We will switch to terminal and see the output.

[Highlight]

All elements after replacement: Debian Redhat Mandriva Fedora

This displays all elements after Ubuntu was replaced with Mandriva
Switch to slides
===== Display Slide 8 =====

“Add an element to an Array”


Syntax:

ArrayName=("${ArrayName[@]}" "New_word_1" "New_word_2")


We can append an element using the following syntax -


ArrayName equal to opening round bracket within double quote dollar symbol opening curly bracket ArrayName opening square bracket At sign closing square bracket closing curly bracket .

space within double quote New_Word_1 space within double quote New_Word_2 closing round bracket.

Let us understand this with the help of our example.

Switch to the code file.

Highlight

Linux=("${Linux[@]}" "Suse")

The highlighted command will append a new element Suse to the array Linux
echo -e "All elements After appending Suse: ${Linux[@]} \n" Then we will echo all the elements after appending Suse.Again till here we can execute and show.
So this will be executed and shown, right?

I am not clear what is intended here.The command highlighted is actual output. Previously we had 4 elements, then we added 'Suse' and the end of the list making it total of 5 elements. The output is the list of all five elements

Switch to terminal and see the output
[Highlight]

All elements After appending Suse: Debian Redhat Mandriva Fedora Suse

Here we can see all the elements after appending Suse to the array
Now, come back to the slides
Display Slide 9

Remove an element from an Array


unset ArrayName[Index number]


An element can be removed from an array by using the following syntax -


Unset space ArrayName opening square bracket index number closing square bracket.

Let us switch to the code file.
Highlight

unset Linux[2]

Here we are using the unset command.


And we will remove the third element Mandriva from the array Linux.

echo -e "All elements after removal of third element: ${Linux[@]} \n" Then we will echo all the elements again after the removal of Mandriva.
Same question as before.Reply to Nancy Varkey (08/08/2013, 11:20): "..."

Here we have removed one element i.e. Mandriva making to count back to 4. The output will show the remaining list of elementsSwitch to terminal and observe the output

All elements after removal of third element: Debian Redhat Fedora Suse Here is the list of elements after removing Mandriva


<PAUSE>

Let us summarize.
Display slide 10

Summary

In this tutorial, we learned to
  • Extract an element from an Array
  • Replace an element in an Array
  • Add element to an Array
  • Remove element from an Array


Display Slide 11

Assignment

As an assignment.
  1. Declare an array names of length 7 and perform following operations
  • extract three elements starting from index two
  • Replace third element with 'XXX' and display all the elements
  • Append any new name at the end of Array


Display Slide 12 Watch the video available at the link shown below

It summarizes the Spoken Tutorial project

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

Display Slide 13


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 16

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

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