Unix Technical Forum

cat << EOF

This is a discussion on cat << EOF within the Slackware Linux Support forums, part of the Unix Operating Systems category; --> simple question... cat << EOF foo bar EOF echos foo and bar... The question is - is that the ...


Go Back   Unix Technical Forum > Unix Operating Systems > Slackware Linux Support

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-20-2008, 09:08 AM
ray
 
Posts: n/a
Default cat << EOF

simple question...

cat << EOF
foo
bar
EOF

echos foo and bar...

The question is - is that the shell (bash) or ??? that supports that
function?

Simple question I'm sure, but googling for << doesn't work....


Either zsh supports that as well or it's not bash... I'm confused.

Ray
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-20-2008, 09:08 AM
Lew Pitcher
 
Posts: n/a
Default Re: cat << EOF

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ray wrote:
> simple question...
>
> cat << EOF
> foo
> bar
> EOF
>
> echos foo and bar...
>
> The question is - is that the shell (bash) or ??? that supports that
> function?
>
> Simple question I'm sure, but googling for << doesn't work....
>
>
> Either zsh supports that as well or it's not bash... I'm confused.


It's called a "here document". Google for that term, and you'll get a
much better explanation than I can give.

But, in brief
it is a feature of the shell; bash supports it, and (apparently) so does
zsh.

The construct
<<something
data
data
something

tells the shell to collect all the lines following the <<something, up
to but not including the trailing 'something' and feed that data into
the processes stdin.

Shell variable expansion will be performed, given the suitable set up of
your shell.

So, for instance,
DATE=`date`
cat >/etc/issue.net <<EOF
Hello there. This file was built on $DATE
You have reached my system. Nice, isnt it?
EOF

will expand the $DATE field, and copy the resulting text into your
/etc/issue.net file

See bash(1) for details
You might also read http://www.faqs.org/docs/abs/HTML/here-docs.html




- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFCpcxBagVFX4UWr64RAtZOAKDaUuMJ0VlaDKv0blEIHx zAUeKp5wCfciyr
t5H9+178SZVGPkQggFx/gFw=
=gc6C
-----END PGP SIGNATURE-----
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-20-2008, 09:08 AM
ray
 
Posts: n/a
Default Re: cat << EOF

Lew Pitcher wrote:

> It's called a "here document". Google for that term, and you'll get a
> much better explanation than I can give.
>


> See bash(1) for details
> You might also read http://www.faqs.org/docs/abs/HTML/here-docs.html
>

awesome, thanks!

I've used it in perl, but didn't know bash supported it (until recently)
and didn't know the name of it.

I'm a windows batch file guy at work... moving to perl and bash has been
both enlighting and confusing and wonderful and overwhelming at the same
time.
(cygwin and perl are starting to show up in my scripts at work...)

Ray
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-20-2008, 09:08 AM
Grant Coady
 
Posts: n/a
Default Re: cat << EOF

On Tue, 07 Jun 2005 12:33:07 -0400, Lew Pitcher <Lew.Pitcher@td.com> wrote:
>
> So, for instance,
> DATE=`date`
> cat >/etc/issue.net <<EOF
> Hello there. This file was built on $DATE
> You have reached my system. Nice, isnt it?
> EOF
>
> will expand the $DATE field, and copy the resulting text into your
> /etc/issue.net file


Did you actualy _try_ that? Indented EOF needs a '-' elsewhere

I'll let you find elsewhere...

--Grant.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-20-2008, 09:08 AM
Lew Pitcher
 
Posts: n/a
Default Re: cat << EOF

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Grant Coady wrote:
> On Tue, 07 Jun 2005 12:33:07 -0400, Lew Pitcher <Lew.Pitcher@td.com> wrote:
>
>>So, for instance,
>> DATE=`date`
>> cat >/etc/issue.net <<EOF
>> Hello there. This file was built on $DATE
>> You have reached my system. Nice, isnt it?
>> EOF
>>
>>will expand the $DATE field, and copy the resulting text into your
>>/etc/issue.net file

>
>
> Did you actualy _try_ that? Indented EOF needs a '-' elsewhere
>
> I'll let you find elsewhere...
>
> --Grant.
>


FWIW, in usenet posts and emails, I typically indent code examples by
two characters to highlight them /as code examples/. The indentation (in
the post) isn't meant to be copied literally.

As for the '-', then
DATE=`date`
cat >/etc/issue.net <<-EOF
Hello there. This file was built on $DATE
You have reached my system. Nice, isnt it?
EOF

To quote bash(1)
"If the redirection operator is <<-, then all leading tab
characters are stripped from input lines and the line con-
taining delimiter. This allows here-documents within
shell scripts to be indented in a natural fashion."





- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFCpfuyagVFX4UWr64RAhhoAJ4uMX8LY3OiinNzSKtIXO 1mYFAbegCcDg5d
bSG3zEBrQmoKmLG7h9mTvn8=
=5Ge8
-----END PGP SIGNATURE-----
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-20-2008, 09:08 AM
Eef Hartman
 
Posts: n/a
Default Re: cat << EOF

ray <nospam@example.com> wrote:
> The question is - is that the shell (bash) or ??? that supports that
> function?


It is actually a generic shell function, any self-respecting shell
should support it (and that includes bash, tcsh, ksh and even the
original Bourne shell:
<<[-]word The shell input is read up to a line that is the
same as word, or to an end-of-file. The resulting
document becomes the standard input. If any
character of word is quoted, no interpretation is
placed upon the characters of the document;
otherwise, parameter and command substitution
occurs, (unescaped) \new-line is ignored, and \
must be used to quote the characters \, $, `, and
the first character of word. If - is appended to
<<, all leading tabs are stripped from word and
from the document.
from the man page for the Bourne shell on a HP-UX - real Unix SystemV -
workstation).

As people already told you, it's called a "here document", as it was
invented to put the input for a command INTO the script itself (the
input document is "here").
--
************************************************** ******************
** Eef Hartman, Delft University of Technology, dept. EWI/TW **
** e-mail: E.J.M.Hartman@math.tudelft.nl, fax: +31-15-278 7295 **
** snail-mail: P.O. Box 5031, 2600 GA Delft, The Netherlands **
************************************************** ******************
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-20-2008, 09:08 AM
notbob
 
Posts: n/a
Default Re: cat << EOF

On 2005-06-07, ray <nospam@example.com> wrote:
> simple question...
>
> cat << EOF
> foo
> bar
> EOF
>
> echos foo and bar...
>
> The question is - is that the shell (bash) or ??? that supports that
> function?


Yes, bash and other scripting languages. Here's some good info:

http://linuxcommand.org/wss0030.php

nb

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-20-2008, 09:08 AM
Grant Coady
 
Posts: n/a
Default Re: cat << EOF

On Tue, 07 Jun 2005 15:55:30 -0400, Lew Pitcher <Lew.Pitcher@td.com> wrote:
>
> FWIW, in usenet posts and emails, I typically indent code examples by
> two characters to highlight them /as code examples/. The indentation (in
> the post) isn't meant to be copied literally.


My apologies, sometimes I shouldn't answer posts half-asleep, but it
might confuse reader if it not work after copy/paste
>
> As for the '-', then
> DATE=`date`
> cat >/etc/issue.net <<-EOF
> Hello there. This file was built on $DATE
> You have reached my system. Nice, isnt it?
> EOF


Would've used echo, but then, with so many ways to do things,
'correct' defined as 'does it do the expected?' Not style.
I tend to think of 'here' documents as data arrays, similar
syntax to reading a file in shell script. Only because that's
the sort of utility I been writing lately. Didn't pick on
your backticks )

--Grant.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-20-2008, 09:08 AM
Lew Pitcher
 
Posts: n/a
Default Re: cat << EOF

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Grant Coady wrote:
> On Tue, 07 Jun 2005 15:55:30 -0400, Lew Pitcher <Lew.Pitcher@td.com> wrote:
>
>>FWIW, in usenet posts and emails, I typically indent code examples by
>>two characters to highlight them /as code examples/. The indentation (in
>>the post) isn't meant to be copied literally.

>
>
> My apologies, sometimes I shouldn't answer posts half-asleep, but it
> might confuse reader if it not work after copy/paste


No problem. No offence meant and none taken.

I hadn't thought of the cut'n'paste angle. Perhaps I had better put sentinals
around code examples to facilitate that sort of thing.

>
>>As for the '-', then
>> DATE=`date`
>> cat >/etc/issue.net <<-EOF
>> Hello there. This file was built on $DATE
>> You have reached my system. Nice, isnt it?
>> EOF

>
>
> Would've used echo, but then, with so many ways to do things,
> 'correct' defined as 'does it do the expected?' Not style.
> I tend to think of 'here' documents as data arrays, similar
> syntax to reading a file in shell script. Only because that's
> the sort of utility I been writing lately. Didn't pick on
> your backticks )


Again, not a problem. I was trying for a /simple/ example to show that shell
variable substitution was handled in the here document. P'haps I didn't pick
as transparent an example as I should have.

I should have pointed the OP at an article I wrote for The Linux Gazette that
had a good example of the use of a here document. In
http://linuxgazette.net/105/pitcher1.html I have a piece of shell script that
updates my ISP's webserver with my homepage. The script invokes FTP with a
here document (crude, but it does what I wanted) to transfer the built html
page to my ISP's server. The FTP script looks like
#-------------code example----------
#<--- left margin ------

TEMPFILE=`/usr/bin/mktemp /tmp/$1=XXXXXX` || exit 2

# ... elided logic to build html homepage into $TEMPFILE

ISP_ADDRESS=webserver.isp.com
ISP_USERID=username
ISP_PASSWD=password

/bin/ftp -n <<STOP
open $ISP_ADDRESS
user $ISP_USERID $ISP_PASSWD
ascii
put $TEMPFILE index.htm
bye
STOP

#<--- left margin ------
#-------------code example----------


> --Grant.



- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFCpjHuagVFX4UWr64RApFnAJwIely6puvc/wu+Ta/73Sd6Y5hYbACgmkRL
oFJxT5YC1DgUdWs7W0xv/Ss=
=biBp
-----END PGP SIGNATURE-----
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-20-2008, 09:08 AM
+Alan Hicks+
 
Posts: n/a
Default Re: cat << EOF

Lew Pitcher <Lew.Pitcher@td.com> trolled:

pgp trash troll delete

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> ray wrote:
>> simple question...
>>
>> cat << EOF
>> foo
>> bar
>> EOF
>>
>> echos foo and bar...
>>
>> The question is - is that the shell (bash) or ??? that supports that
>> function?
>>
>> Simple question I'm sure, but googling for << doesn't work....
>>
>>
>> Either zsh supports that as well or it's not bash... I'm confused.

>
> It's called a "here document". Google for that term, and you'll get a
> much better explanation than I can give.
>
> But, in brief
> it is a feature of the shell; bash supports it, and (apparently) so does
> zsh.
>
> The construct
> <<something
> data
> data
> something
>
> tells the shell to collect all the lines following the <<something, up
> to but not including the trailing 'something' and feed that data into
> the processes stdin.
>
> Shell variable expansion will be performed, given the suitable set up of
> your shell.
>
> So, for instance,
> DATE=`date`
> cat >/etc/issue.net <<EOF
> Hello there. This file was built on $DATE
> You have reached my system. Nice, isnt it?
> EOF
>
> will expand the $DATE field, and copy the resulting text into your
> /etc/issue.net file
>
> See bash(1) for details
> You might also read http://www.faqs.org/docs/abs/HTML/here-docs.html
>
>
>
>
> - --
>
> Lew Pitcher, IT Specialist, Enterprise Data Systems
> Enterprise Technology Solutions, TD Bank Financial Group
>
> (Opinions expressed here are my own, not my employer's)
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.4 (MingW32)
>
> iD8DBQFCpcxBagVFX4UWr64RAtZOAKDaUuMJ0VlaDKv0blEIHx zAUeKp5wCfciyr
> t5H9+178SZVGPkQggFx/gFw=
> =gc6C
> -----END PGP SIGNATURE-----

_________________________________________
Usenet Zone Free Binaries Usenet Server
More than 120,000 groups
Unlimited download
http://www.usenetzone.com to open account
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:45 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com