Difference between revisions of "BASH/C3/Here-document-and-Here-string/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
Line 6: | Line 6: | ||
|- | |- | ||
| 00:01 | | 00:01 | ||
− | | Dear friends, | + | | Dear friends, welcome to the '''spoken tutorial''' on''' HERE document and strings'''. |
|- | |- | ||
Line 14: | Line 14: | ||
|- | |- | ||
| 00:11 | | 00:11 | ||
− | | | + | | special-purpose redirection called '''Here documents '''and '''Here strings''' |
|- | |- | ||
| 00:17 | | 00:17 | ||
− | | | + | | with the help of some examples. |
|- | |- | ||
| 00:20 | | 00:20 | ||
− | | To follow this tutorial you should have knowledge of '''Shell Scripting''' in''' BASH.''' | + | | To follow this tutorial, you should have knowledge of '''Shell Scripting''' in''' BASH.''' |
|- | |- | ||
| 00:26 | | 00:26 | ||
− | |If not, for relevant tutorials please visit our website which is as shown | + | |If not, for relevant tutorials, please visit our website which is as shown. '''(http://www.spoken-tutorial.org)''' |
|- | |- | ||
| 00:32 | | 00:32 | ||
− | | For this tutorial I am using | + | | For this tutorial, I am using: |
|- | |- | ||
Line 42: | Line 42: | ||
|- | |- | ||
| 00:42 | | 00:42 | ||
− | |Please note,''' GNU Bash | + | |Please note,''' GNU Bash''' version 4 or above is recommended for practice. |
|- | |- | ||
Line 50: | Line 50: | ||
|- | |- | ||
| 00:52 | | 00:52 | ||
− | |* It is a special | + | |* It is a special purpose block of text or code. |
|- | |- | ||
Line 66: | Line 66: | ||
|- | |- | ||
| 01:10 | | 01:10 | ||
− | |* It can also be treated as multiple line input redirected to a shell script. | + | |* It can also be treated as multiple line input redirected to a '''shell script'''. |
|- | |- | ||
| 01:17 | | 01:17 | ||
− | | Syntax is | + | | Syntax is- |
|- | |- | ||
Line 78: | Line 78: | ||
|- | |- | ||
| 01:24 | | 01:24 | ||
− | |After this on the next line, we can give the text | + | |After this, on the next line, we can give the '''text input'''s. |
|- | |- | ||
Line 86: | Line 86: | ||
|- | |- | ||
| 01:33 | | 01:33 | ||
− | |Here, '''text1, text2, textN''' are the text | + | |Here, '''text1, text2, textN''' are the '''text input'''s. |
|- | |- | ||
Line 102: | Line 102: | ||
|- | |- | ||
| 01:53 | | 01:53 | ||
− | |I will open a file named '''here dot sh ''' | + | |I will open a file named '''here dot sh '''. |
|- | |- | ||
Line 110: | Line 110: | ||
|- | |- | ||
| 02:04 | | 02:04 | ||
− | | Let me put a block of code after this line. | + | | Let me put a '''block''' of '''code''' after this line. |
|- | |- | ||
| 02:09 | | 02:09 | ||
− | | wc represents word count | + | | '''wc''' represents word count. |
|- | |- | ||
| 02:12 | | 02:12 | ||
− | | '''wc hyphen w counts the number of words in the '''HERE''' document | + | | '''wc hyphen w''' counts the number of words in the '''HERE''' document. |
|- | |- | ||
| 02:20 | | 02:20 | ||
− | |The block of code or text until the second | + | |The block of code or text until the second occurrence of '''HERE''' will be treated as a file. |
|- | |- | ||
| 02:28 | | 02:28 | ||
− | |The content present in '''HERE''' document is an input to the command '''wc hyphen w''' | + | |The content present in '''HERE''' '''document''' is an '''input''' to the command '''wc hyphen w'''. |
|- | |- | ||
| 02:36 | | 02:36 | ||
− | | '''HERE''' acts as a delimiter for '''wc hyphen w''' command while reading multi-line input. | + | | '''HERE''' acts as a '''delimiter''' for '''wc hyphen w''' command while reading multi-line input. |
|- | |- | ||
| 02:47 | | 02:47 | ||
− | |If we try to execute the same command in the '''terminal''', we should get ''''4'''' as an output. | + | |If we try to '''execute''' the same command in the '''terminal''', we should get ''''4'''' as an output. |
|- | |- | ||
Line 142: | Line 142: | ||
|- | |- | ||
| 03:03 | | 03:03 | ||
− | |Now click on '''Save''' to save the file. | + | |Now click on '''Save''' to '''save''' the file. |
|- | |- | ||
Line 158: | Line 158: | ||
|- | |- | ||
| 03:24 | | 03:24 | ||
− | |Type '''dot slash here dot sh''' | + | |Type: '''dot slash here dot sh''' |
|- | |- | ||
Line 166: | Line 166: | ||
|- | |- | ||
| 03:30 | | 03:30 | ||
− | | We can see the output as '''4 | + | | We can see the output as '''4''' |
|- | |- | ||
| 03:33 | | 03:33 | ||
− | | | + | |that is, number of words in '''Here document '''is '''4.''' |
|- | |- | ||
Line 182: | Line 182: | ||
|- | |- | ||
| 03:47 | | 03:47 | ||
− | |'''Hello and welcome to Bash learning''' | + | |'''Hello and welcome to Bash learning'''. |
|- | |- | ||
Line 190: | Line 190: | ||
|- | |- | ||
| 03:54 | | 03:54 | ||
− | |Let us execute the program again. | + | |Let us '''execute''' the program again. |
|- | |- | ||
| 03:57 | | 03:57 | ||
− | | On the '''terminal | + | | On the '''terminal''', type: dot slash here dot sh''' |
|- | |- | ||
Line 206: | Line 206: | ||
|- | |- | ||
| 04:13 | | 04:13 | ||
− | | We can also pass an argument to the '''Here document.''' | + | | We can also pass an '''argument''' to the '''Here document.''' |
|- | |- | ||
Line 214: | Line 214: | ||
|- | |- | ||
| 04:22 | | 04:22 | ||
− | |Let me open a file named '''hereoutput dot sh''' | + | |Let me open a file named '''hereoutput dot sh'''. |
|- | |- | ||
| 04:28 | | 04:28 | ||
− | | The command '''cat''' will concatenate files and print the standard output. | + | | The command '''cat''' will concatenate files and '''print''' the '''standard output'''. |
|- | |- | ||
| 04:35 | | 04:35 | ||
− | | Note that we have used the string '''this''' instead of '''HERE.''' | + | | Note that we have used the '''string''' '''"this" ''' instead of '''"HERE".''' |
|- | |- | ||
Line 230: | Line 230: | ||
|- | |- | ||
| 04:47 | | 04:47 | ||
− | |You can use any other delimiter also. | + | |You can use any other '''delimiter''' also. |
|- | |- | ||
| 04:51 | | 04:51 | ||
− | | This line will display the | + | | This line will display the 0th (zeroth) '''argument'''. |
|- | |- | ||
| 04:55 | | 04:55 | ||
− | |The | + | |The 0th (zeroth) '''argument''', by default, is the '''filename'''. |
|- | |- | ||
| 05:00 | | 05:00 | ||
− | | This line will display the ''' | + | | This line will display the 1st '''argument''' passed to the program. |
|- | |- | ||
| 05:05 | | 05:05 | ||
− | | And this line will display the ''' | + | | And this line will display the 2nd '''argument''' passed to the program. |
|- | |- | ||
| 05:09 | | 05:09 | ||
− | | Here we have to close the document using the same delimiter, '''this.''' | + | | Here we have to close the '''document''' using the same '''delimiter''', '''this.''' |
|- | |- | ||
| 05:17 | | 05:17 | ||
− | | Save the file. | + | | '''Save''' the file. |
|- | |- | ||
| 05:18 | | 05:18 | ||
− | | Let us now execute the program. | + | | Let us now '''execute''' the program. |
|- | |- | ||
| 05:21 | | 05:21 | ||
− | |On the '''terminal''' type: '''chmod space plus x space hereoutput dot sh''' | + | |On the '''terminal''', type: '''chmod space plus x space hereoutput dot sh'''. |
|- | |- | ||
Line 270: | Line 270: | ||
|- | |- | ||
| 05:32 | | 05:32 | ||
− | |Type '''dot slash hereoutput dot sh space Sunday space Monday''' | + | |Type: '''dot slash hereoutput dot sh space Sunday space Monday''' |
|- | |- | ||
| 05:40 | | 05:40 | ||
− | | The output is | + | | The '''output''' is displayed as: |
|- | |- | ||
| 05:43 | | 05:43 | ||
− | |'''0'th argument is: dot salsh hereoutput dot sh ''' which is the '''filename.''' | + | |'''"0'th argument is: dot salsh hereoutput dot sh" ''' which is the '''filename.''' |
|- | |- | ||
Line 286: | Line 286: | ||
|- | |- | ||
| 05:51 | | 05:51 | ||
− | |'''2nd argument is: Monday ''' | + | |'''2nd argument is: Monday '''. |
|- | |- | ||
| 05:55 | | 05:55 | ||
− | | Let us learn about '''Here string''' now. | + | | Let us learn about''' "Here" string''' now. |
|- | |- | ||
| 05:59 | | 05:59 | ||
− | |* '''Here string '''is used for input redirection from text or a variable. | + | |* '''Here string '''is used for '''input redirection''' from '''text''' or a '''variable'''. |
|- | |- | ||
| 06:06 | | 06:06 | ||
− | |* The input is mentioned in the same line within single quotes. | + | |* The '''input''' is mentioned in the same line within single quotes. |
|- | |- | ||
| 06:12 | | 06:12 | ||
− | | The syntax is | + | | The syntax is- '''command''' space three 'less than symbols' space within single quotes write '''string''' |
|- | |- | ||
| 06:22 | | 06:22 | ||
Line 309: | Line 309: | ||
|- | |- | ||
| 06:25 | | 06:25 | ||
− | |I will open the same file '''here dot sh''' | + | |I will open the same file '''here dot sh'''. |
|- | |- | ||
| 06:30 | | 06:30 | ||
− | | Here at the end, I will type: '''wc space hyphen w three less than symbols space | + | | Here at the end, I will type: '''wc space hyphen w''' three less than symbols space within single quotes '''Welcome to Bash learning'''. |
|- | |- | ||
| 06:44 | | 06:44 | ||
− | |This will redirect the string within quotes to the command '''wc hyphen w.''' | + | |This will '''redirect''' the '''string''' within quotes to the command '''wc hyphen w.''' |
|- | |- | ||
| 06:52 | | 06:52 | ||
− | |Now | + | |Now click on '''Save '''to '''save''' the changes. |
|- | |- | ||
Line 329: | Line 329: | ||
|- | |- | ||
| 06:58 | | 06:58 | ||
− | | Now type: '''dot slash here dot sh''' | + | | Now type: '''dot slash here dot sh'''. |
|- | |- | ||
| 07:03 | | 07:03 | ||
− | | We can see the output as '''6''' and '''4''' | + | | We can see the output as '''6''' and '''4'''. |
|- | |- | ||
Line 349: | Line 349: | ||
|- | |- | ||
| 07:23 | | 07:23 | ||
− | |Let us | + | |Let us summarize. |
|- | |- | ||
| 07:25 | | 07:25 | ||
− | |In this tutorial we learnt about | + | |In this tutorial, we learnt about: |
|- | |- | ||
Line 361: | Line 361: | ||
|- | |- | ||
| 07:29 | | 07:29 | ||
− | |* '''HERE string''' | + | |* '''HERE string'''. |
|- | |- | ||
| 07:31 | | 07:31 | ||
− | | As an assignment, convert a string to uppercase using: | + | | As an assignment, convert a '''string''' to uppercase using: |
|- | |- | ||
Line 373: | Line 373: | ||
|- | |- | ||
| 07:37 | | 07:37 | ||
− | |* '''Here string''' | + | |* '''Here string'''. |
|- | |- | ||
Line 381: | Line 381: | ||
|- | |- | ||
| 07:47 | | 07:47 | ||
− | |This is the command to convert characters from lower to upper case. | + | |This is the '''command''' to convert characters from lower to upper case. |
|- | |- | ||
Line 389: | Line 389: | ||
|- | |- | ||
| 07:57 | | 07:57 | ||
− | |It | + | |It summarizes the Spoken-Tutorial project. |
|- | |- | ||
Line 397: | Line 397: | ||
|- | |- | ||
| 08:06 | | 08:06 | ||
− | | The Spoken Tutorial | + | | The Spoken Tutorial project team: Conducts workshops using spoken tutorials. |
|- | |- | ||
| 08:12 | | 08:12 | ||
− | | Gives certificates to those who pass an online test | + | | Gives certificates to those who pass an online test. |
|- | |- | ||
Line 409: | Line 409: | ||
|- | |- | ||
| 08:25 | | 08:25 | ||
− | | Spoken Tutorial Project is a part of the Talk to a Teacher project. | + | | '''Spoken Tutorial''' Project is a part of the '''Talk to a Teacher''' project. |
|- | |- | ||
Line 417: | Line 417: | ||
|- | |- | ||
| 08:38 | | 08:38 | ||
− | |More information on this | + | |More information on this mission is available at the link shown below. |
+ | ''' http://spoken-tutorial.org\NMEICT-Intro''' | ||
|- | |- | ||
Line 425: | Line 426: | ||
|- | |- | ||
| 08:50 | | 08:50 | ||
− | |This is Ashwini Patil from IIT Bombay. | + | |This is Ashwini Patil from IIT Bombay. signing off |
|- | |- |
Revision as of 08:58, 7 July 2015
Time | Narration |
00:01 | Dear friends, welcome to the spoken tutorial on HERE document and strings. |
00:08 | In this tutorial, we will learn about |
00:11 | special-purpose redirection called Here documents and Here strings |
00:17 | with the help of some examples. |
00:20 | To follow this tutorial, you should have knowledge of Shell Scripting in BASH. |
00:26 | If not, for relevant tutorials, please visit our website which is as shown. (http://www.spoken-tutorial.org) |
00:32 | For this tutorial, I am using: |
00:34 | * Ubuntu Linux 12.04 Operating System |
00:39 | * GNU BASH version 4.2 |
00:42 | Please note, GNU Bash version 4 or above is recommended for practice. |
00:49 | Let us learn about Here document. |
00:52 | * It is a special purpose block of text or code. |
00:56 | * It is a form of I/O redirect. |
01:00 | * It feeds a command list to an interactive program or command line. |
01:06 | * It can be treated as a separate file. |
01:10 | * It can also be treated as multiple line input redirected to a shell script. |
01:17 | Syntax is- |
01:18 | command space less than less than space HERE. |
01:24 | After this, on the next line, we can give the text inputs. |
01:29 | It can consist of any number of lines. |
01:33 | Here, text1, text2, textN are the text inputs. |
01:40 | After the text inputs, on the next line, we type the keyword HERE again. |
01:46 | It denotes the closing of HERE document. |
01:50 | Let us understand this with an example. |
01:53 | I will open a file named here dot sh . |
01:59 | The first line of code is the shebang line. |
02:04 | Let me put a block of code after this line. |
02:09 | wc represents word count. |
02:12 | wc hyphen w counts the number of words in the HERE document. |
02:20 | The block of code or text until the second occurrence of HERE will be treated as a file. |
02:28 | The content present in HERE document is an input to the command wc hyphen w. |
02:36 | HERE acts as a delimiter for wc hyphen w command while reading multi-line input. |
02:47 | If we try to execute the same command in the terminal, we should get '4' as an output. |
02:55 | This is because we have passed four words to the command 'wc hyphen w'. |
03:03 | Now click on Save to save the file. |
03:06 | Let us switch to the Terminal using Ctrl, Alt and T keys simultaneously on your keyboard. |
03:15 | Type: chmod space plus x space here dot sh |
03:22 | Press Enter. |
03:24 | Type: dot slash here dot sh |
03:27 | Press Enter. |
03:30 | We can see the output as 4 |
03:33 | that is, number of words in Here document is 4. |
03:38 | Come back to the program. |
03:41 | Let us add two more words here at the beginning of the text. |
03:47 | Hello and welcome to Bash learning. |
03:52 | Click on Save. |
03:54 | Let us execute the program again. |
03:57 | On the terminal, type: dot slash here dot sh |
04:04 | Press Enter. |
04:06 | Now the output is 6 because we added two more words to our text. |
04:13 | We can also pass an argument to the Here document. |
04:18 | Let us see how to do this with an example. |
04:22 | Let me open a file named hereoutput dot sh. |
04:28 | The command cat will concatenate files and print the standard output. |
04:35 | Note that we have used the string "this" instead of "HERE". |
04:41 | It’s not necessary that you always have to use the delimiter HERE. |
04:47 | You can use any other delimiter also. |
04:51 | This line will display the 0th (zeroth) argument. |
04:55 | The 0th (zeroth) argument, by default, is the filename. |
05:00 | This line will display the 1st argument passed to the program. |
05:05 | And this line will display the 2nd argument passed to the program. |
05:09 | Here we have to close the document using the same delimiter, this. |
05:17 | Save the file. |
05:18 | Let us now execute the program. |
05:21 | On the terminal, type: chmod space plus x space hereoutput dot sh. |
05:29 | Press Enter |
05:32 | Type: dot slash hereoutput dot sh space Sunday space Monday |
05:40 | The output is displayed as: |
05:43 | "0'th argument is: dot salsh hereoutput dot sh" which is the filename. |
05:49 | 1st argument is: Sunday |
05:51 | 2nd argument is: Monday . |
05:55 | Let us learn about "Here" string now. |
05:59 | * Here string is used for input redirection from text or a variable. |
06:06 | * The input is mentioned in the same line within single quotes. |
06:12 | The syntax is- command space three 'less than symbols' space within single quotes write string |
06:22 | Let us understand this with an example. |
06:25 | I will open the same file here dot sh. |
06:30 | Here at the end, I will type: wc space hyphen w three less than symbols space within single quotes Welcome to Bash learning. |
06:44 | This will redirect the string within quotes to the command wc hyphen w. |
06:52 | Now click on Save to save the changes. |
06:55 | We will switch to the Terminal. |
06:58 | Now type: dot slash here dot sh. |
07:03 | We can see the output as 6 and 4. |
07:08 | Number of words in here document is 6 and number of words in here string is 4. |
07:15 | Likewise, you can write your own Here strings. |
07:20 | This brings us to the end of this tutorial. |
07:23 | Let us summarize. |
07:25 | In this tutorial, we learnt about: |
07:27 | * HERE document |
07:29 | * HERE string. |
07:31 | As an assignment, convert a string to uppercase using: |
07:36 | * Here document |
07:37 | * Here string. |
07:39 | Hint: tr space a hyphen z space capital A hyphen capital Z. |
07:47 | This is the command to convert characters from lower to upper case. |
07:54 | Watch the video available at the link shown below. |
07:57 | It summarizes the Spoken-Tutorial project. |
08:01 | If you do not have good bandwidth, you can download and watch it. |
08:06 | The Spoken Tutorial project team: Conducts workshops using spoken tutorials. |
08:12 | Gives certificates to those who pass an online test. |
08:17 | For more details, please write to contact@spoken-tutorial.org |
08:25 | Spoken Tutorial Project is a part of the Talk to a Teacher project. |
08:29 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
08:38 | More information on this mission is available at the link shown below. |
08:44 | The script has been contributed by FOSSEE and Spoken-Tutorial teams. |
08:50 | This is Ashwini Patil from IIT Bombay. signing off |
08:54 | Thank you for joining. |