Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 21st, 2006, 09:54 AM
dan_track dan_track is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 19 dan_track User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 28 m 44 sec
Reputation Power: 0
What does EOF do?

Hi

I saw this script and I'm not sure why or how it works:

cat >> test << STOP
line 1
line 2
line 3
STOP

or equally

cat >> test << EOF
line 1
line 2
line 3
EOF

Is this an OS issue or a bash issue?

Can anyone help explain this to me.

Thanks
Dan

Reply With Quote
  #2  
Old February 21st, 2006, 10:20 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,390 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 32 m 40 sec
Reputation Power: 4080
That is known as a "Here Tag". Basically it tells the shell that you are going to enter a multi-line string until "Here". You could call it anything you want, not just EOF or STOP.
Code:
[mb@ironmaiden mb]$ cat > test <<HERE
> Hello
> world
> HERE

The <<HERE part tells the shell that you're going to enter multi-lines until the HERE tag. Thus the shell waits until it sees HERE to consider the rest of the stuff above it as input for cat.

Some rules about the Here tags:
1. The tag can be any string, uppercase or lowercase, though most people use uppercase by convention.
2. The tag will not be considered as a Here tag if there are other words in that line. In this case, it will merely be considered part of the string. The tag should be by itself on a separate line, to be considered a tag.
3. The tag should have no leading or trailing spaces in that line to be considered a tag. Otherwise it will be considered as part of the string.
Here's another chunk to consider:
Code:
[mb@ironmaiden mb]$ cat >> test <<HERE
> Hello world HERE <--- Not the end of string
> This is a test
>  HERE <-- Leading space, so not end of string
> and a new line
> HERE <-- Now we have the end of the string

Note that in the above, we have the word HERE showing up 3 times. The first two times, it occurs with other words or leading spaces and hence it is considered as part of the string.

Many scripting languages (perl, PHP, awk, ruby etc.) support the concept of the "Here tag", so it isn't unique to bash alone.
Comments on this post
Annie79 agrees: Best explanation I read on this so far.
B-Con agrees: Tripped across this in a Google search. Good explanation.
kk5st agrees: Likewise, what B-Con said (9/19/11)
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Reply With Quote
  #3  
Old February 21st, 2006, 10:29 AM
dan_track dan_track is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 19 dan_track User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 28 m 44 sec
Reputation Power: 0
what does EOF do?

Hey thanks for that. Much appreciated.

All of it makes sense. Although why does the following work?

cat <<EOF>> filename

how is that order isn't important, I mean I could have :

cat >> filename <<EOF

and this would still work, how does bash know where to start from?

Thanks for your help with this.
Dan

Reply With Quote
  #4  
Old February 21st, 2006, 01:19 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,390 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 32 m 40 sec
Reputation Power: 4080
That's because when you enter the command, bash parses the first line and sees that it needs to run cat and redirect the output to a file called filename. It also sees that the input is going to come from STDIN (i.e. the terminal) and that it should read input until it sees your "here" tag. The bash parser can accept input/output redirection operators in any order really.

Reply With Quote
  #5  
Old March 16th, 2009, 04:26 AM
Sidorenko Sidorenko is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 1 Sidorenko User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 19 sec
Reputation Power: 0
Quote:
Originally Posted by Scorpions4ever
Many scripting languages (perl, PHP, awk, ruby etc.) support the concept of the "Here tag", so it isn't unique to bash alone.


Hi Scorpions4ever

Could you please provide an example, how HERE tag can be used in awk? I've tried
{
print <<EOF
Hi there!
EOF
}
But it seems not to work. TIA

Reply With Quote
  #6  
Old November 1st, 2011, 02:06 AM
randombits randombits is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2011
Posts: 1 randombits User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 22 sec
Reputation Power: 0
Using HERE tags with awk

Hi,
You can use HERE tags with awk like the example below...

bash Code:
Original - bash Code
  1. ls -li > temp
  2. awk -f - temp <<'HERE'
  3.         {
  4.                 print $1;
  5.         }
  6. HERE

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > What does EOF do?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap