Difference between revisions of "BASH/C2/Array-Operations-in-BASH/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
Line 17: | Line 17: | ||
| 00:05 | | 00:05 | ||
| In this tutorial, we will learn how to: | | In this tutorial, we will learn how to: | ||
− | * Declare an ''' | + | * Declare an ''' array''' and assign values to it. |
|- | |- | ||
| 00:12 | | 00:12 | ||
− | | * Initialize an''' | + | | * Initialize an''' array''' during declaration. |
|- | |- | ||
| 00:15 | | 00:15 | ||
− | | * To find length of an ''' | + | | * To find length of an ''' array''' and its n<sup>th</sup> '''element'''. |
|- | |- | ||
| 00:20 | | 00:20 | ||
− | | * To print an ''' | + | | * To print an '''array'''. |
|- | |- | ||
Line 57: | Line 57: | ||
|- | |- | ||
| 00:50 | | 00:50 | ||
− | |Let us start with the definition of an ''' | + | |Let us start with the definition of an ''' array''' and its characteristics. |
|- | |- | ||
| 00:55 | | 00:55 | ||
− | | * An ''' | + | | * An '''array''' is a '''variable''' with multiple '''values'''. |
|- | |- | ||
Line 81: | Line 81: | ||
|- | |- | ||
| 01:16 | | 01:16 | ||
− | | We will now see how to declare and assign a '''value''' to an ''' | + | | We will now see how to declare and assign a '''value''' to an '''array'''. |
|- | |- | ||
| 01:21 | | 01:21 | ||
− | | The syntax to declare an ''' | + | | The syntax to declare an '''array''' is- |
|- | |- | ||
| 01:24 | | 01:24 | ||
− | | '''declare hyphen | + | | '''declare hyphen 'a' arrayname''' |
|- | |- | ||
| 01:28 | | 01:28 | ||
− | | '''“declare”''' keyword is used to declare an ''' | + | | '''“declare”''' keyword is used to declare an '''array'''. |
|- | |- | ||
Line 101: | Line 101: | ||
|- | |- | ||
| 01:35 | | 01:35 | ||
− | | The syntax to assign a value to an ''' | + | | The syntax to assign a value to an '''array '''is- |
|- | |- | ||
Line 109: | Line 109: | ||
|- | |- | ||
| 01:46 | | 01:46 | ||
− | | Now, let see how to initialize an ''' | + | | Now, let see how to initialize an '''array''' during declaration. |
|- | |- | ||
Line 125: | Line 125: | ||
|- | |- | ||
| 02:03 | | 02:03 | ||
− | | The syntax is '''declare hyphen | + | | The syntax is '''declare hyphen 'a' arrayname equal to within round brackets within single quotes 'element1' , 'element2'''' and '''element3''' . |
|- | |- | ||
Line 137: | Line 137: | ||
|- | |- | ||
| 02:28 | | 02:28 | ||
− | | Now Type: '''gedit''' '''space array.sh''' space & | + | | Now Type: '''gedit''' '''space array.sh''' space ampersand (&) |
|- | |- | ||
| 02:36 | | 02:36 | ||
− | | We use the ampersand to free up the prompt. Press '''Enter'''. | + | | We use the ampersand to free up the '''prompt'''. Press '''Enter'''. |
|- | |- | ||
| 02:41 | | 02:41 | ||
− | | Type the '''code''' as shown here, in your | + | | Type the '''code''' as shown here, in your "array.sh" file. |
|- | |- | ||
| 02:47 | | 02:47 | ||
− | | This line declares an ''' | + | | This line declares an '''array''' named '''Linux''' with elements - |
− | + | "Debian", "Redhat", "Ubuntu" and "Fedora". | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 02:57 | | 02:57 | ||
− | | Here hyphen | + | | Here hyphen 'a' is a '''flag.''' |
|- | |- | ||
| 03:00 | | 03:00 | ||
− | | It allows us to read and assign '''values''' to an ''' | + | | It allows us to read and assign '''values''' to an '''array.''' |
|- | |- | ||
| 03:05 | | 03:05 | ||
− | | Let us switch back to | + | | Let us switch back to our '''slides'''. |
|- | |- | ||
| 03:07 | | 03:07 | ||
− | | The length of an ''' | + | | The length of an '''array''' can be obtained by this syntax: |
|- | |- | ||
| 03:12 | | 03:12 | ||
− | | Dollar sign opening curly bracket hash '''arrayname''' within square | + | | Dollar sign ($) opening curly bracket hash '''arrayname''' within square brackets "At" sign (@) and closing curly bracket. |
|- | |- | ||
| 03:22 | | 03:22 | ||
− | | The length of the n | + | | The length of the n<sup>th</sup> '''element''' can be obtained by this syntax: |
|- | |- | ||
| 03:28 | | 03:28 | ||
− | | Dollar sign opening curly bracket hash arrayname within square brackets | + | | Dollar sign opening curly bracket hash '''arrayname''' within square brackets 'n' and closing curly bracket. |
|- | |- | ||
Line 190: | Line 186: | ||
|- | |- | ||
| 03:42 | | 03:42 | ||
− | | All the elements of the ''' | + | | All the elements of the '''array''' can be printed using this syntax. |
|- | |- | ||
| 03:48 | | 03:48 | ||
− | | Dollar sign opening curly bracket '''arrayname''' within square brackets | + | | Dollar sign opening curly bracket '''arrayname''' within square brackets 'At' sign (@) and closing curly bracket. |
|- | |- | ||
| 03:57 | | 03:57 | ||
− | | Now come back to | + | | Now come back to our '''text editor.''' |
|- | |- | ||
| 04:00 | | 04:00 | ||
− | | This line will display total number of elements in the ''' | + | | This line will display total number of elements in the '''array Linux'''. |
|- | |- | ||
| 04:06 | | 04:06 | ||
− | | ' | + | | hyphen 'e' enables interpretation of backslash escapes. |
|- | |- | ||
| 04:11 | | 04:11 | ||
− | | We have included this as we have ' | + | | We have included this as we have backslash 'n' at the end of the line. |
|- | |- | ||
| 04:18 | | 04:18 | ||
− | | The next line displays all the elements of the ''' | + | | The next line displays all the elements of the '''array Linux.''' |
|- | |- | ||
| 04:23 | | 04:23 | ||
− | | This line displays the 3<sup>rd</sup> element of the ''' | + | | This line displays the 3<sup>rd</sup> element of the '''array Linux.''' |
|- | |- | ||
| 04:28 | | 04:28 | ||
− | | Please note that an ''' | + | | Please note that an '''array''' always starts with '''index''' zero. |
|- | |- | ||
Line 234: | Line 230: | ||
|- | |- | ||
| 04:42 | | 04:42 | ||
− | | Let's first make the file executable by typing '''chmod''' space '''plus x space array.sh'''. Press Enter. | + | | Let's first make the file executable by typing '''chmod''' space '''plus x space array.sh'''. Press '''Enter'''. |
|- | |- | ||
Line 242: | Line 238: | ||
|- | |- | ||
| 05:01 | | 05:01 | ||
− | | The output is displayed. | + | | The '''output''' is displayed. |
|- | |- | ||
| 05:04 | | 05:04 | ||
− | |The number of elements or the length of the ''' | + | |The number of elements or the length of the '''array 'Linux' ''' is four. |
|- | |- | ||
| 05:10 | | 05:10 | ||
− | | The elements of the ''' | + | | The elements of the '''array Linux''' are '''Debian, Redhat, Ubuntu and Fedora.''' |
|- | |- | ||
| 05:18 | | 05:18 | ||
− | | The | + | | The third element of '''array Linux''' is ''' Ubuntu'''. |
|- | |- | ||
Line 270: | Line 266: | ||
|- | |- | ||
| 05:35 | | 05:35 | ||
− | | In this tutorial, we | + | | In this tutorial, we learned to: |
|- | |- | ||
| 05:40 | | 05:40 | ||
− | | * Declare and assign values to an ''' | + | | * Declare and assign values to an '''array''' |
|- | |- | ||
| 05:43 | | 05:43 | ||
− | |* Initialize an ''' | + | |* Initialize an '''array''' during declaration |
|- | |- | ||
| 05:46 | | 05:46 | ||
− | |* To find length of an ''' | + | |* To find length of an '''array''' and its n<sup>th</sup> element and |
|- | |- | ||
| 05:51 | | 05:51 | ||
− | | * To print whole ''' | + | | * To print whole '''array'''. |
|- | |- | ||
Line 306: | Line 302: | ||
|- | |- | ||
| 06:04 | | 06:04 | ||
− | |* | + | |* and print the 5<sup>th</sup> element. |
|- | |- | ||
Line 326: | Line 322: | ||
|- | |- | ||
| 06:20 | | 06:20 | ||
− | | Conducts workshops using spoken tutorials. | + | |* Conducts workshops using spoken tutorials. |
|- | |- | ||
| 06:24 | | 06:24 | ||
− | | Gives certificates to those who pass an online test. | + | |* Gives certificates to those who pass an online test. |
|- | |- | ||
Line 338: | Line 334: | ||
|- | |- | ||
| 06:35 | | 06:35 | ||
− | |Spoken Tutorial Project is a part of the | + | |'''Spoken Tutorial''' Project is a part of the '''Talk to a Teacher''' project. |
|- | |- |
Revision as of 10:41, 10 July 2015
Title of script: Array Operations In BASH
Author: Lavitha Pereira
Keywords: Video tutorial, Bash shell, Array
Time | Narration |
---|---|
00:01 | Welcome to the spoken tutorial on Array operations in BASH. |
00:05 | In this tutorial, we will learn how to:
|
00:12 | * Initialize an array during declaration. |
00:15 | * To find length of an array and its nth element. |
00:20 | * To print an array. |
00:22 | To follow this tutorial, You should be familiar with Linux Operating System. |
00:27 | For relevant tutorials, please visit: spoken hyphen tutorial dot org. |
00:33 | For this tutorial, I am using: |
00:37 | * Ubuntu Linux 12.04 OS |
00:41 | * GNU Bash version 4.1.10 |
00:45 | GNU Bash version 4 or above is recommended for practice. |
00:50 | Let us start with the definition of an array and its characteristics. |
00:55 | * An array is a variable with multiple values. |
01:01 | * The values can be of same or different types. |
01:04 | * There is no maximum limit for the size of an array. |
01:08 | * Array members need not be consecutive. |
01:12 | * Array index always starts with zero. |
01:16 | We will now see how to declare and assign a value to an array. |
01:21 | The syntax to declare an array is- |
01:24 | declare hyphen 'a' arrayname |
01:28 | “declare” keyword is used to declare an array. |
01:31 | It is a built-in command in Bash. |
01:35 | The syntax to assign a value to an array is- |
01:38 | Name within square brackets index equals to within single quotes value. |
01:46 | Now, let see how to initialize an array during declaration. |
01:51 | * Array can be declared and initialized at the same time. |
01:56 | * Elements Should be separated by a space . |
02:00 | Each element should be within parentheses. |
02:03 | The syntax is declare hyphen 'a' arrayname equal to within round brackets within single quotes 'element1' , 'element2' and element3 . |
02:19 | Let us try an example. |
02:21 | Open the terminal by pressing Ctrl+Alt and T keys simultaneously. |
02:28 | Now Type: gedit space array.sh space ampersand (&) |
02:36 | We use the ampersand to free up the prompt. Press Enter. |
02:41 | Type the code as shown here, in your "array.sh" file. |
02:47 | This line declares an array named Linux with elements -
"Debian", "Redhat", "Ubuntu" and "Fedora". |
02:57 | Here hyphen 'a' is a flag. |
03:00 | It allows us to read and assign values to an array. |
03:05 | Let us switch back to our slides. |
03:07 | The length of an array can be obtained by this syntax: |
03:12 | Dollar sign ($) opening curly bracket hash arrayname within square brackets "At" sign (@) and closing curly bracket. |
03:22 | The length of the nth element can be obtained by this syntax: |
03:28 | Dollar sign opening curly bracket hash arrayname within square brackets 'n' and closing curly bracket. |
03:37 | Here 'n' is the element number whose length is to be found. |
03:42 | All the elements of the array can be printed using this syntax. |
03:48 | Dollar sign opening curly bracket arrayname within square brackets 'At' sign (@) and closing curly bracket. |
03:57 | Now come back to our text editor. |
04:00 | This line will display total number of elements in the array Linux. |
04:06 | hyphen 'e' enables interpretation of backslash escapes. |
04:11 | We have included this as we have backslash 'n' at the end of the line. |
04:18 | The next line displays all the elements of the array Linux. |
04:23 | This line displays the 3rd element of the array Linux. |
04:28 | Please note that an array always starts with index zero. |
04:34 | Lastly, this line displays the number of characters present in the 3rd element. |
04:40 | Now, switch to the Terminal. |
04:42 | Let's first make the file executable by typing chmod space plus x space array.sh. Press Enter. |
04:56 | Type: dot slash array .sh. Press Enter. |
05:01 | The output is displayed. |
05:04 | The number of elements or the length of the array 'Linux' is four. |
05:10 | The elements of the array Linux are Debian, Redhat, Ubuntu and Fedora. |
05:18 | The third element of array Linux is Ubuntu. |
05:22 | And the number of characters in the third element is six, as expected. |
05:29 | This brings us to the end of this tutorial. |
05:32 | Let us summarize. Come back to our slides. |
05:35 | In this tutorial, we learned to: |
05:40 | * Declare and assign values to an array |
05:43 | * Initialize an array during declaration |
05:46 | * To find length of an array and its nth element and |
05:51 | * To print whole array. |
05:53 | As an assignment- |
05:55 | Declare an array names of length 7 and find: |
06:00 | * the total number of elements |
06:02 | * print all the elements |
06:04 | * and print the 5th element. |
06:06 | Watch the video available at the link shown below. |
06:10 | It summarizes the Spoken-Tutorial project. |
06:13 | If you do not have good bandwidth, you can download and watch it. |
06:18 | The Spoken Tutorial Project team: |
06:20 | * Conducts workshops using spoken tutorials. |
06:24 | * Gives certificates to those who pass an online test. |
06:27 | For more details, please write to contact@spoken-tutorial.org. |
06:35 | Spoken Tutorial Project is a part of the Talk to a Teacher project. |
06:40 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
06:47 | More information on this mission is available at the link shown below. |
06:52 | The script has been contributed by FOSSEE and spoken-tutorial teams. |
06:58 | This is Ashwini Patil from IIT Bombay. |
07:02 | Thank you for joining. |