BASH/C3/Here-document-and-Here-string/English-timed
From Script | Spoken-Tutorial
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-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 our 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. 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'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. |