BASH/C3/Here-document-and-Here-string/English

From Script | Spoken-Tutorial
Revision as of 12:29, 20 August 2014 by Gaurav (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

­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