LinuxQuestions.org
Help answer threads with 0 replies.
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 08-13-2011, 05:53 AM   #1
UNGR
LQ Newbie
 
Registered: Aug 2011
Location: Denmark
Distribution: Ubuntu
Posts: 8

Rep: Reputation: Disabled
Need an explanation of here scripts as well as the "cat" command


Hi everyone!

I am currently in the very early stages of learning how to use the shell and write scripts (and I mean really early - only been at it for 3 days or so).

Now I am following a tutorial, and I have reached a section about the so-called "here scripts" or "here documents".

In the tutorial, we have the following script:

Code:
#!/bin/bash

# make_page - A script to produce an HTML file

echo "<HTML>"
echo "<HEAD>"
echo "  <TITLE>"
echo "  The title of your page"
echo "  </TITLE>"
echo "</HEAD>"
echo ""
echo "<BODY>"
echo "  Your page content goes here."
echo "</BODY>"
echo "</HTML>"
Which we then make a "here script" of like this to save us from all the repeated echos:

Code:
#!/bin/bash

# make_page - A script to produce an HTML file

cat << _EOF_
<HTML>
<HEAD>
    <TITLE>
    The title of your page
    </TITLE>
</HEAD>

<BODY>
    Your page content goes here.
</BODY>
</HTML>
_EOF_
Now what I don't understand is how those two scripts, once run, produce the same output.. I believe this is because I don't understand the "cat"-command (which is not quite useful, considering it seems to be a very practical and widely used command).

So would anyone care to explain to me what it does in this specific script as well as alternative applications? (And please refrain from just quoting the man-pages.. I have read them multiple times, and I still just don't get it - It's probably very simple, but please bear with me!)

Thank you in advance!
 
Old 08-13-2011, 05:58 AM   #2
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Linux Mint
Posts: 8,511

Rep: Reputation: 884Reputation: 884Reputation: 884Reputation: 884Reputation: 884Reputation: 884Reputation: 884
Basically it tells the shell that you are going to enter a multi-line string until "_EOF_". You could call it anything you want, not just EOF or STOP.

Kind regards
 
Old 08-13-2011, 06:53 AM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,568
Blog Entries: 1

Rep: Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026Reputation: 1026
You can also do variable substitution within the document. This should illustrate it nicely:

Code:
#!/bin/bash

# make_page - A script to produce an HTML file
PAGE="Cats and Dogs"

cat << _EOF_
<HTML>
<HEAD>
    <TITLE>
    $PAGE
    </TITLE>
</HEAD>

<BODY>
    <h1>Hi, My name is $1</h1>
</BODY>
</HTML>
_EOF_
If you run this script with one parameter (ie. your name):

./script Martin

$1 is called a positional parameter as it returns the first parameter provided with the script.
 
Old 08-13-2011, 07:01 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,562

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
So I find it interesting that you say you are doing a tutorial and yet it tells you what to do but nothing about the why (odd).

Anyway, this may help explain further.
 
Old 08-13-2011, 07:29 AM   #5
UNGR
LQ Newbie
 
Registered: Aug 2011
Location: Denmark
Distribution: Ubuntu
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
So I find it interesting that you say you are doing a tutorial and yet it tells you what to do but nothing about the why (odd).

Anyway, this may help explain further.
It does explain it - I just didn't understand the explanation
 
Old 08-13-2011, 07:33 AM   #6
UNGR
LQ Newbie
 
Registered: Aug 2011
Location: Denmark
Distribution: Ubuntu
Posts: 8

Original Poster
Rep: Reputation: Disabled
So that explained the here document... Now the second part: What exactly does the "cat" command do, and why does it produce the same output as "echo" in this case? (Again, I have read the man pages, though I didn't quite understand it.. Perhaps one of you would be so kind as to dumb it down? Remember, I only started learning this 3 days ago, so go easy!)
 
Old 08-13-2011, 07:42 AM   #7
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Linux Mint
Posts: 8,511

Rep: Reputation: 884Reputation: 884Reputation: 884Reputation: 884Reputation: 884Reputation: 884Reputation: 884
Did you even read the link?
Quote:
Here, someprogram can be any program that reads from standard input (cat is by far the most common), and WORD can be any delimiter word you like. (EOF is a common choice.)
Kind regards
 
Old 08-13-2011, 08:14 AM   #8
UNGR
LQ Newbie
 
Registered: Aug 2011
Location: Denmark
Distribution: Ubuntu
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by repo View Post
Did you even read the link?


Kind regards
I certainly did, but I stress again that I am COMPLETELY new to this... "A program that reads from a standard input" isn't really what I'd call dumbing it down, as I kindly asked for throughout this thread.. Would you mind giving me some basic examples of what exactly the cat command is, and how it is used? (Also why it produces the same output as "echo" in the above example)

I apologize if there is anything I overlooked in the link, but I don't see any explanations of the cat command - only what the here scripts are and what to do with 'em

Again, thanks for your time!

Last edited by UNGR; 08-13-2011 at 08:16 AM.
 
Old 08-13-2011, 08:20 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
Cat very simply copies standard in (or a file) to standard out. Think of it as a kind of pipe version of echo. It prints out everything you feed into it.

It's often used to "concatenate" several files together, as sending multiple inputs into it will give you one continuous output, but it's very useful in scripting as well. It can do a few filtering actions too. Read the man page.

A heredoc simply sets a block of text apart, as if it existed in a separate file, and spits it out as if you were using a < redirect. Using cat with it thus prints the contents.

Note that cat is an external command, /bin/cat, while the heredoc is a shell built-in feature.
 
Old 08-13-2011, 08:25 AM   #10
UNGR
LQ Newbie
 
Registered: Aug 2011
Location: Denmark
Distribution: Ubuntu
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
Cat very simply copies standard in (or a file) to standard out. Think of it as a kind of pipe version of echo. It prints out everything you feed into it.

It's often used to "concatenate" several files together, as sending multiple inputs into it will give you one continuous output, but it's very useful in scripting as well. It can do a few filtering actions too. Read the man page.

A heredoc simply sets a block of text apart, as if it existed in a separate file, and spits it out as if you were using a < redirect. Using cat with it thus prints the contents.

Note that cat is an external command, /bin/cat, while the heredoc is a shell built-in feature.
Aha! That's exactly the response I was looking for - covered all my questions! Thank you very much, sir!

And thanks to everyone else who answered as well

SOLVED
 
Old 08-13-2011, 09:56 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
Glad to help out. Keep on scripting!

Come to think of it, I wonder why bash has never bothered to create a built-in version of cat. You'd think it would be useful not to have to spawn an external process just to print out some stuff, and it should be easy enough to implement.

Does any other shell have a cat built-in?

It might then even be possible to set it up so that redirections without receiving commands, like "<file.txt" or a heredoc alone, would go directly to stdout, as if there were an implied cat in front of it (controllable with a shell option, of course).

Sorry, just thinking out loud.
 
Old 08-13-2011, 10:08 AM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by David the H. View Post
Come to think of it, I wonder why bash has never bothered to create a built-in version of cat. You'd think it would be useful not to have to spawn an external process just to print out some stuff, and it should be easy enough to implement.
I agree, that probably would be a good idea.

Quote:
Originally Posted by David the H. View Post
It might then even be possible to set it up so that redirections without receiving commands, like "<file.txt" or a heredoc alone, would go directly to stdout, as if there were an implied cat in front of it (controllable with a shell option, of course).
As far as I know, it already does that inside command substitution (such as "$(< path/to/file)"), but not when you just write a command. I wonder why.
 
  


Reply


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
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
xorg.conf : Explanation of "Viewport" and "Virtual" uncle-c Linux - Newbie 1 01-24-2011 11:04 AM
"cat" command not working iValentine22 Linux - Newbie 2 10-25-2010 12:19 AM
script using "/usr/bin/cat error" produces "cannot open" in cron Dcrusoe Programming 6 07-22-2009 03:30 PM
Feeding the output of "diff" or "cat" command to dpkg --purge kushalkoolwal Debian 9 06-19-2008 07:27 AM


All times are GMT -5. The time now is 08:41 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration