Difference between revisions of "BASH/C3/Here-document-and-Here-string/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(One intermediate revision by one other user not shown)
Line 41: Line 41:
 
* '''GNU BASH''' version 4.2
 
* '''GNU BASH''' version 4.2
  
Please note,''' GNU Bash''' '''version 4''' or above is recommended to practice this tutorial.
+
Please note,''' GNU Bash''' '''version 4''' or above is recommended for practice.
  
 
|-
 
|-
Line 57: Line 57:
 
* It feeds a command list to an interactive program or command line.
 
* It feeds a command list to an interactive program or command line.
 
* It can be treated as a separate file.
 
* It can be treated as a separate file.
* It can also be treated as multiple line input redirected to a command or a shell script.
+
* It can also be treated as multiple line input redirected to a shell script.
 
+
  
  
Line 75: Line 74:
  
 
HERE
 
HERE
 
  
  
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Syntax is,
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Syntax is,
  
'''command''' '''space''' '''less than''' '''less than''' '''space HERE'''.
+
'''command space less than less than space HERE'''.
  
  
After this on the next line, we can give the text input.  
+
After this on the next line, we can give the text inputs.  
  
  
 
It can consist of any number of lines.
 
It can consist of any number of lines.
  
Here, '''text1, text2, textN''' are the text input.
+
 
 +
Here, '''text1, text2, textN''' are the text inputs.
  
  
Line 105: Line 104:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''<nowiki>#!/usr/bin/env bash </nowiki>'''
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''<nowiki>#!/usr/bin/env bash </nowiki>'''
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The first line of code is the s'''hebang''' line.
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The first line of code is the '''shebang''' line.
  
 
|-
 
|-
Line 117: Line 116:
  
 
'''HERE'''
 
'''HERE'''
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let me put a block of text after this line.
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let me put a block of code after this line.
  
 
<nowiki>wc -w << HERE </nowiki>
 
<nowiki>wc -w << HERE </nowiki>
Line 133: Line 132:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight '''<nowiki>wc -w << HERE </nowiki>'''
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight '''<nowiki>wc -w << HERE </nowiki>'''
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| * '''wc '''represents 'word count'
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|  
 +
* '''wc '''represents 'word count'
 
* '''wc hyphen w '''counts the number of words in the '''Here''' '''document.'''
 
* '''wc hyphen w '''counts the number of words in the '''Here''' '''document.'''
 
  
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight the block of text
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight the block of text
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| * The block of text until the second occurance of '''HERE, '''will be treated as a file.
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|  
 +
* The block of text until the second occurance of '''HERE, '''will be treated as a file.
 
* The content present in '''HERE document''' is an input to the command '''wc -w'''
 
* The content present in '''HERE document''' is an input to the command '''wc -w'''
 
  
  
Line 150: Line 149:
  
  
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.
  
  
 
This is because we have passed four words to the command ''''wc -c''''.  
 
This is because we have passed four words to the command ''''wc -c''''.  
 +
 +
Now click on '''Save''' to save the file.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Switch to '''Terminal'''
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Switch to '''Terminal'''
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us switch to the '''Terminal.'''
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us switch to the '''Terminal''' using '''Ctrl, Alt''' and '''T''' keys simultaneously on your keyboard.
  
 
|-
 
|-
Line 165: Line 166:
  
  
'''dot slash here.sh>>'''P'''ress Enter'''
+
'''dot slash here.sh>>'''Press '''Enter'''
  
  
Line 200: Line 201:
  
  
Type '''Hello and'''
+
Type '''Hello and welcome to Bash learning'''
  
 
|-
 
|-
Line 211: Line 212:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| On the '''terminal, '''type'''./here.sh'''
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| On the '''terminal, '''type'''./here.sh'''
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| On the '''terminal, '''press the up-arrow key and recall the previous command.
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| On the '''terminal, type '''./here.sh'''
 
+
'''./here.sh'''
+
  
  
Line 252: Line 251:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''0'th argument is : $0 '''
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''0'th argument is : $0 '''
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This line will display the '''0th''' (zeroeth) argument.
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This line will display the '''0th (zeroeth) argument'''.
  
The 0th (zeroeth) argument, by default, is the filename.
+
The '''0th (zeroeth) argument''', by default, is the '''filename'''.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''1st argument is $1 '''
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''1st argument is $1 '''
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This line will display the 1st argument passed to the program.
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This line will display the '''1st argument''' passed to the program.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''2nd argument is $2 '''
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''2nd argument is $2 '''
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| And this line will display the 2nd argument passed to the program.
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| And this line will display the '''2nd argument''' passed to the program.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''this'''
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''this'''
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Here we have to close the document using the same delimiter, '''this.'''
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Here we have to close the document using the same delimiter, '''this.'''
 
 
  
  
Line 282: Line 279:
 
On the '''terminal''' type:
 
On the '''terminal''' type:
  
'''chmod''' space '''+x''' space '''hereoutput.sh'''
+
'''chmod space +x space hereoutput.sh'''
  
 
'''./hereoutput.sh Sunday Monday'''
 
'''./hereoutput.sh Sunday Monday'''
Line 290: Line 287:
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The output is dispalyed as:
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The output is dispalyed as:
  
'''The invoked script name: '''
+
'''0'th argument is: ./hereoutput.sh ''' which is the '''filename.'''
 
+
'''0'th argument is: ./hereoutput.sh '''
+
  
 
'''1st argument is: Sunday'''
 
'''1st argument is: Sunday'''
Line 306: Line 301:
  
 
'''Here String'''
 
'''Here String'''
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us learn about '''Here '''string now.
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us learn about '''Here string''' now.
 
+
  
 
* '''Here string '''is used for input redirection from text or a variable.
 
* '''Here string '''is used for input redirection from text or a variable.
 
* The input is mentioned in the same line within single quotes.
 
* The input is mentioned in the same line within single quotes.
 
  
  
Line 320: Line 313:
  
 
'''<nowiki>command <<< 'string'</nowiki>'''
 
'''<nowiki>command <<< 'string'</nowiki>'''
 
 
 
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The syntax is,
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The syntax is,
  
'''command''' space '''three less than''' symbols space within '''single quotes write string'''
+
'''command space three less than symbols space''' within single quotes write '''string'''
  
 
|-
 
|-
Line 333: Line 323:
  
 
I will open the same file '''here.sh'''
 
I will open the same file '''here.sh'''
 
'''<nowiki>wc -w <<< 'Welcome to Bash learning'</nowiki>'''
 
 
 
 
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| <nowiki>wc -w <<< 'Welcome to Bash learning'</nowiki>
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| <nowiki>wc -w <<< 'Welcome to Bash learning'</nowiki>
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Here at the end I will type:
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Here at the end, I will type:
 
+
'''wc space hyphen w three less then symbols'''
+
  
'''<nowiki>(<<<) space within quote 'Welcome to Bash learning'</nowiki>'''
+
'''wc space hyphen w three less than symbols space''' within quotes '''Welcome to Bash learning'''
  
  
This will redirect the string within quotes to''' '''the command '''wc -w.'''
+
This will redirect the string within quotes to the command '''wc -w.'''
  
  
Click on '''Save '''to save changes.
+
Click on '''Save '''to save the changes.
  
 
|-
 
|-
Line 369: Line 352:
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''<nowiki>[Highlight]</nowiki>'''
 
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''<nowiki>[Highlight]</nowiki>'''
  
'''Output''''''6'''
+
'''Output'''
 +
 
 +
'''6'''
  
 
'''4'''
 
'''4'''
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We can see the output as 6 and 4
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| 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.'''
+
Number of words in '''here document''' is '''6''' and number of words in '''here string''' is''' 4.'''
  
 
|-
 
|-
Line 399: Line 381:
 
* '''HERE document'''
 
* '''HERE document'''
 
* '''HERE string'''  
 
* '''HERE string'''  
 
 
  
 
|-
 
|-
Line 406: Line 386:
  
 
'''Assignment'''
 
'''Assignment'''
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| # Convert a string to uppercase using:
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| As an assignment, convert a string to uppercase using:
  
 
* '''Here document'''
 
* '''Here document'''
 
* '''Here string'''
 
* '''Here string'''
  
Hint:''' tr a-z A-Z''' is the command to convert characters from lower to upper case.
+
Hint:''' tr a-z A-Z'''.
 +
 
 +
This is the command to convert characters from lower to upper case.
  
 
|-
 
|-

Latest revision as of 12:29, 20 August 2014

­Title of script: HERE document and strings

Author: Lavitha Pereira and Ashwini Patil

Keywords: Video tutorial, redirection, here string, here document


Visual Cue
Narration
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
  • special-purpose redirection called Here documents and Here strings
  • With the help of some examples


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
  • Ubuntu Linux 12.04 Operating System and
  • GNU BASH version 4.2

Please note, GNU Bash version 4 or above is recommended for practice.

Display slide 5

Here documents


Let us learn about Here document.


  • It is a special-purpose block of text or code.
  • It is a form of I/O redirect.
  • It feeds a command list to an interactive program or command line.
  • It can be treated as a separate file.
  • It can also be treated as multiple line input redirected to a shell script.


Display silde 6

Syntax:

command << HERE

text1

text2

textN

HERE


Syntax is,

command space less than less than space HERE.


After this on the next line, we can give the text inputs.


It can consist of any number of lines.


Here, text1, text2, textN are the text inputs.


After the text inputs, on the next line, we type the keyword HERE again.


It denotes the closing of HERE document.

Let us understand this with an example.


I will open a file named here.sh

#!/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
  • wc represents 'word count'
  • wc hyphen w counts the number of words in the Here document.


Highlight the block of text
  • The block of text until the second occurance of HERE, will be treated as a file.
  • The content present in HERE document is an input to the command wc -w


wc -w << HERE HERE acts as a delimiter for wc -w command while reading multi-line input.


If we try to execute the same command in the terminal, we should get '4' as an output.


This is because we have passed four words to the command 'wc -c'.

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


dot slash here.sh>>Press Enter


And type:chmod space plus x space here dot sh

Press Enter.


Type dot slash here dot sh

Press Enter.

[Highlight]

'Output'4

We can see the output is 4.


That is, number of words in Here document 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.


Type Hello and welcome to Bash learning

Click on Save. Click on Save.


Let us execute the program again.

On the terminal, type./here.sh On the terminal, type ./here.sh


Press Enter.

Now the output is 6 because we added two more words to our text.


<<PAUSE>>

We can also pass an argument to the Here document.
Open hereoutput.sh file. Let us see how to do this with an example.


Let me open a file named hereoutput.sh

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.


It’s not necessary that you always have to use the delimiter HERE.


You can use any other delimiter also.

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.


On the terminal type:

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.
  • Here string is used for input redirection from text or a variable.
  • The input is mentioned in the same line within single quotes.


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.


I will open the same file here.sh

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


This will redirect the string within quotes to the command wc -w.


Click on Save to save the changes.

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

  • HERE document
  • HERE string
Display slide 15

Assignment

As an assignment, convert a string to uppercase using:
  • Here document
  • Here string

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.


It summarises the Spoken Tutorial project.


If you do not have good bandwidth, you can download and watch it.

Display Slide 17

Spoken Tutorial Workshops

The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates to those who pass an online test

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.


It is supported by the National Mission on Education through ICT, MHRD, Government of India.


More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro

Display Slide 19 The script has been contributed by FOSSEE and Spoken-Tutorial teams.


This is Ashwini from IIT Bombay.

Thank you for joining.

Contributors and Content Editors

Ashwini, Gaurav, Nancyvarkey