LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-08-2013, 04:22 AM   #1
xeechou
Member
 
Registered: Jun 2013
Location: China
Distribution: Archlinux
Posts: 67

Rep: Reputation: Disabled
about "eof"


I'm reading a book about linux and I saw a line of bash command goes like that
cat > catfile << "eof"
Well, the book say that, If I type "eof" after what I wanna input in catfile, the input is down, but I have no idea what the "eof" in the bash cmd did? As a input file? I dont think so. Are there any config file can give me any clue what exactly happened, any help is appreciated.
 
Old 07-08-2013, 04:34 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
The 'eof' part harkens back to End Of File, but in the case you are looking at it can actually be any word and it is creating a Here Document (more here) and it simply tells
where the input to the document ends
 
1 members found this post helpful.
Old 07-08-2013, 04:37 AM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
the << means stdin input, not from a file. here this common, and pretty weird looking, command means to take the stdin stream until it sees the string "eof". When it sees that, it will stop writing to the file (and not include that string in the file. It's a nifty way to write files out from a script file....

Code:
cat > catfile << "eof"
this string goes into catfile
so does this string
and this one too
until i write
eof

# we are now back to standard shell script world...
cat catfile
 
Old 07-08-2013, 04:56 AM   #4
xeechou
Member
 
Registered: Jun 2013
Location: China
Distribution: Archlinux
Posts: 67

Original Poster
Rep: Reputation: Disabled
re:grail

Quote:
Originally Posted by grail View Post
The 'eof' part harkens back to End Of File, but in the case you are looking at it can actually be any word and it is creating a Here Document (more here) and it simply tells
where the input to the document ends
thanks alot about Here Document feature, bash has alot features...
 
Old 07-08-2013, 05:11 AM   #5
mddnix
Member
 
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 521

Rep: Reputation: 141Reputation: 141
Its same as pressing CTRL + D in interactive mode.
Code:
cat > catfile
some
text
goes
here
<CTRL + D>
In non-interactive mode, Here Document (<<eof) is generally used to display menus or sending mails.

For example:
PHP Code:
#!/bin/bash

cat <<-EOF
    1
Show how many files.
    
2Show where I am now.
    
3Date please..
    
4Bye Bye
EOF

read ans
case "$ansin
    1
ls -wc -l
       
;;
    
2pwd
       
;;
    
3date
       
;;
    *) echo 
"Bye Bye!!!"
       
exit 0
       
;;
esac 

Last edited by mddnix; 07-08-2013 at 05:16 AM.
 
Old 07-08-2013, 06:18 AM   #6
xeechou
Member
 
Registered: Jun 2013
Location: China
Distribution: Archlinux
Posts: 67

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
The 'eof' part harkens back to End Of File, but in the case you are looking at it can actually be any word and it is creating a Here Document (more here) and it simply tells
where the input to the document ends
So it doesnt matter if it is "eof" or not, just to use it to close the input?
 
Old 07-08-2013, 06:33 AM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
yes, it can be any string, just make sure there's no way it could appear sooner in the body of text. "_EOF_" is the most common string I'd say.
 
Old 07-08-2013, 08:10 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
As a side note, I also like to use the here document to comment out portions of code while testing:
Code:
echo testing new feature

:<<COMMENT
if <something here>
then
    echo new feature works
else
   ...
fi
COMMENT

echo this is my existing code
The alternative is to place a '#' in front of each line you need to comment out
 
  


Reply

Tags
bash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] cat > filename << "EOF" syntax explanation bucovaina78 Linux - General 4 12-21-2011 03:50 PM
unpredictable "delete" "move to trash" or "cut" file menu option dorianrenato Linux - General 3 11-28-2011 06:41 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM
UT2004 some maps give "ReadFile beyond EOF 0+4/0..." AC97Conquerer Linux - Games 1 03-25-2005 01:52 AM
bash redirection "$ cat << EOF > file" (how does this work) ninmonkeys Linux - General 1 11-09-2004 03:37 PM


All times are GMT -5. The time now is 03:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration