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 to practice this tutorial. |
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.
Here, text1, text2, textN are the text input.
|
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 text after this line.
wc -w << HERE Welcome to Bash learning HERE |
Highlight wc -w << HERE | * wc represents 'word count'
|
Highlight the block of text | * The block of text until the second occurance of HERE, will be treated as a file.
|
wc -w << HERE | HERE acts as a delimiter for wc -w command while reading multi-line input.
|
Switch to Terminal | Let us switch to the Terminal. |
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, press the up-arrow key and recall the previous command.
./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:
The invoked script name: 0'th argument is: ./hereoutput.sh 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'
| |
wc -w <<< 'Welcome to Bash learning' | Here at the end I will type:
wc space hyphen w three less then symbols (<<<) space within quote '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
|
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 |
# Convert a string to uppercase using:
Hint: tr a-z A-Z 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. |