Difference between revisions of "BASH/C2/More-on-Arrays/English"
(Created page with ''''Title of script: More on Arrays in BASH Shell Scripting''' '''Author:''' Lavitha Pereira '''Keywords: video tutorial, Bash shell, Array''' {| style="border-spacing:0;" | s…') |
|||
Line 215: | Line 215: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| |
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. | 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. |
Revision as of 12:56, 10 November 2014
Title of script: More on Arrays in BASH Shell Scripting
Author: Lavitha Pereira
Keywords: video tutorial, Bash shell, Array
|
|
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
|
Display Slide 4
Prerequisite
|
To follow this tutorial,
You should be familiar with the Linux Operating System.
|
Display Slide 5
System Requirements |
For this tutorial I am using
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
|
Display Slide 6
Syntax:
|
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
|
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.
${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
Original elements in an array Linux: Debian Redhat Ubuntu Fedora
|
We get the output as shown -
|
OUTPUT
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.
|
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. |
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”
ArrayName=("${ArrayName[@]}" "New_word_1" "New_word_2")
|
We can append an element using the following syntax -
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
|
An element can be removed from an array by using the following syntax -
|
Let us switch to the code file. | |
Highlight
unset Linux[2] |
Here we are using the unset command.
|
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
|
Let us summarize. | |
Display slide 10
Summary |
In this tutorial, we learned to
|
Display Slide 11
Assignment |
As an assignment.
|
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
|
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.
Thank you for joining. |