BASH/C3/Here-document-and-Here-string/English
Title of script: HERE document and strings
Author: Lavitha Pereira and Ashwini Patil
Keywords: Video tutorial, redirection, here string, here document
|
|
Display Slide 1 | Dear friends, Welcome to the spoken tutorial on HERE document and strings |
Display Slide 2 | In this tutorial, we will learn about
|
Display Slide 3Prerequisites
|
To follow this tutorial you should have knowledge of Shell Scripting in BASH.
If not, for relevant tutorials please visit our website which is as shown,(http://www.spoken-tutorial.org) |
Display Slide 4
System requirements |
For this tutorial I am using
Please note, GNU Bash version 4 or above is recommended for practice. |
Display slide 5
Here documents
|
Let us learn about Here document.
|
Display silde 6
Syntax: command << HERE text1 text2 textN HERE
|
Syntax is,
command space less than less than space HERE.
|
Let us understand this with an example.
| |
#!/usr/bin/env bash | The first line of code is the shebang line. |
wc -w << HERE
Welcome to Bash learning HERE |
Let me put a block of code after this line.
wc -w << HERE Welcome to Bash learning HERE |
Highlight wc -w << HERE |
|
Highlight the block of text |
|
wc -w << HERE | HERE acts as a delimiter for wc -w command while reading multi-line input.
Now click on Save to save the file. |
Switch to Terminal | Let us switch to the Terminal using Ctrl, Alt and T keys simultaneously on your keyboard. |
Type
chmod +x here.sh>>Press Enter
|
And type:chmod space plus x space here dot sh
Press Enter.
Press Enter. |
[Highlight]
'Output'4 |
We can see the output is 4.
|
On the editor:Type:
Hello and before Welcome |
Come back to the program.
Let us add two more words here at the beginning of the text.
|
Click on Save. | Click on Save.
|
On the terminal, type./here.sh | On the terminal, type ./here.sh
|
Now the output is 6 because we added two more words to our text.
| |
We can also pass an argument to the Here document. | |
Open hereoutput.sh file. | Let us see how to do this with an example.
|
cat << this | The command cat will concatenate files and print the standard output. |
cat << this | Note that we have used the string this instead of HERE.
|
0'th argument is : $0 | This line will display the 0th (zeroeth) argument.
The 0th (zeroeth) argument, by default, is the filename. |
1st argument is $1 | This line will display the 1st argument passed to the program. |
2nd argument is $2 | And this line will display the 2nd argument passed to the program. |
this | Here we have to close the document using the same delimiter, this.
|
Save | Save the file. |
chmod +x hereoutput.sh'./hereoutput.sh Sunday Monday' | Let us now execute the program.
chmod space +x space hereoutput.sh ./hereoutput.sh Sunday Monday |
The output is dispalyed as:
0'th argument is: ./hereoutput.sh which is the filename. 1st argument is: Sunday 2nd argument is: Monday | |
<<PAUSE>> | |
Display Slide 12
Here String |
Let us learn about Here string now.
|
Display Slide 13
Syntax: command <<< 'string' |
The syntax is,
command space three less than symbols space within single quotes write string |
Let us understand this with an example.
| |
wc -w <<< 'Welcome to Bash learning' | Here at the end, I will type:
wc space hyphen w three less than symbols space within quotes Welcome to Bash learning
|
Switch to Terminal | We will switch to the Terminal. |
dot slash here.sh
|
Now type:
./here.sh |
[Highlight]
Output 6 4 |
We can see the output as 6 and 4
Number of words in here document is 6 and number of words in here string is 4. |
Likewise, you can write your own Here strings. | |
This brings us to the end of this tutorial. | |
Display Slide 14
Summary |
Summary
Let us summarise. In this tutorial we learnt about
|
Display slide 15
Assignment |
As an assignment, convert a string to uppercase using:
Hint: tr a-z A-Z. This is the command to convert characters from lower to upper case. |
Display Slide 16
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial About the Spoken Tutorial Project |
Watch the video available at the link shown below.
|
Display Slide 17
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
For more details, please write to contact@spoken-tutorial.org |
Display Slide 18
Acknowledgement |
Spoken Tutorial Project is a part of the Talk to a Teacher project.
|
Display Slide 19 | The script has been contributed by FOSSEE and Spoken-Tutorial teams.
Thank you for joining. |