Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

2.3 Here Documents

This document discusses here documents in shell scripts. Here documents allow including data directly in a shell script by delimiting a multi-line string with special characters like <<! and !. Parameters in the here document are substituted before being passed to the command. Quoting can prevent substitution for specific values or the entire here document. Shell variables can also store string values and are named with alphanumeric characters and underscores.

Uploaded by

arunabhatla
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

2.3 Here Documents

This document discusses here documents in shell scripts. Here documents allow including data directly in a shell script by delimiting a multi-line string with special characters like <<! and !. Parameters in the here document are substituted before being passed to the command. Quoting can prevent substitution for specific values or the entire here document. Shell variables can also store string values and are named with alphanumeric characters and underscores.

Uploaded by

arunabhatla
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

-[xy]) esac

...

The usual quoting conventions apply so that


case $i in \?) ...

will match the character ?.

2.3 Here documents


The shell procedure tel in section 2.1 uses the file /usr/lib/telnos to supply the data for grep. An alternative is to include this data within the shell procedure as a here document, as in,
for i do grep $i <<! ... fred mh0123 bert mh0789 ... ! done

In this example the shell takes the lines between <<! and ! as the standard input for grep. The string ! is arbitrary, the document being terminated by a line that consists of the string following <<.

Parameters are substituted in the document before it is made available to grep as illustrated by the following procedure called edg.
ed $3 <<% g/$1/s//$2/g w %

The call
edg string1 string2 file

is then equivalent to the command


ed file <<% g/string1/s//string2/g w %

and changes all occurrences of string1 in file to string2. Substitution can be prevented using \ to quote the special character $ as in
ed $3 <<+ 1,\$s/$1/$2/g w +

(This version of edg is equivalent to the first except that ed will print a ? if there are no occurrences of the string $1.) Substitution within a here document may be prevented entirely by quoting the terminating string, for example,

grep $i <<\# ... #

The document is presented without modification to grep. If parameter substitution is not required in a here document this latter form is more efficient.

2.4 Shell variables


The shell provides string-valued variables. Variable names begin with a letter and consist of letters, digits and underscores. Variables may be given values by writing, for example,
user=fred box=m000 acct=mh0000

You might also like