LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   about "eof" (https://www.linuxquestions.org/questions/linux-newbie-8/about-eof-4175468839/)

v3ct0r 07-08-2013 04:22 AM

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.

grail 07-08-2013 04:34 AM

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

acid_kewpie 07-08-2013 04:37 AM

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


v3ct0r 07-08-2013 04:56 AM

re:grail
 
Quote:

Originally Posted by grail (Post 4986071)
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...

Madhu Desai 07-08-2013 05:11 AM

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 


v3ct0r 07-08-2013 06:18 AM

Quote:

Originally Posted by grail (Post 4986071)
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?

acid_kewpie 07-08-2013 06:33 AM

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.

grail 07-08-2013 08:10 AM

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


All times are GMT -5. The time now is 05:25 PM.