LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need an explanation of here scripts as well as the "cat" command (http://www.linuxquestions.org/questions/linux-newbie-8/need-an-explanation-of-here-scripts-as-well-as-the-cat-command-897101/)

UNGR 08-13-2011 06:53 AM

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!

repo 08-13-2011 06:58 AM

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

sycamorex 08-13-2011 07:53 AM

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.

grail 08-13-2011 08:01 AM

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.

UNGR 08-13-2011 08:29 AM

Quote:

Originally Posted by grail (Post 4441726)
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 :)

UNGR 08-13-2011 08:33 AM

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!)

repo 08-13-2011 08:42 AM

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

UNGR 08-13-2011 09:14 AM

Quote:

Originally Posted by repo (Post 4441746)
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!

David the H. 08-13-2011 09:20 AM

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.

UNGR 08-13-2011 09:25 AM

Quote:

Originally Posted by David the H. (Post 4441759)
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

David the H. 08-13-2011 10:56 AM

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. ;)

MTK358 08-13-2011 11:08 AM

Quote:

Originally Posted by David the H. (Post 4441832)
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. (Post 4441832)
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.


All times are GMT -5. The time now is 05:54 AM.