LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   simple scripting question (https://www.linuxquestions.org/questions/linux-newbie-8/simple-scripting-question-585266/)

jerf 09-17-2007 08:20 AM

simple scripting question
 
im following a tutorial at linuxcommand.org on how to write basic scripts and do simple things with them.

one of the scripts uses echo
//////////////////////////////////////////////
#!/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>"
///////////////////////////////////////////////////

and the author suggests that a smarter way to achieve the same effect would be to use a 'here script' which in the example uses the 'cat' command and a token,

//////////////////////////////////////////////////////
#!/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_
/////////////////////////////////////////////////////////

my question is about the cat command i guess. how does cat work, and also how does it replace what was achieved by the echo command?

thanks for your help folks.

GrapefruiTgirl 09-17-2007 08:38 AM

Hi!
If you haven't, read the man pages for cat and echo, to see where most of the difference lies (type 'man cat' or 'man echo' in a console)

From my limited experience, there isn't much difference in the functionality of getting info into a file. However, I believe the cat statement(s) would require slightly less code. To show this, consider the following example, which you can do in a console:

Example a)
Code:


echo "Hello, this is a sentence" > file.txt
echo " This is another sentence" >> file.txt
echo "This is sentence number three" >> file.txt
echo "This is the fourth and last sentence" >> file.txt
cat file.txt


Example B)
Code:


cat << EOF > file.txt
>This is sentence one.
>this is sentence two!
>This is sentence number three...
>And finally, number four. Were done.
>EOF
cat file.txt

Note that there is less repetition using cat, because you don't need to use the quotes and the word 'echo' with every line.

Now, with echo however, you could do something like :
Code:


echo -e "This is sentence one \nThis is sentence two\nThis is sentence number 3\nAnd this is sentence number four\n" > file.txt

cat file.txt

And wind up with the same results. The -e switch makes echo interpret escape characters (like \n for newline).
Note that the > means 'make the file if it does not exist, or overwrite it if it exists'
Using >> means 'append to the file if it exists, or make it if it does not exist'

Does this help any?

b0uncer 09-17-2007 08:42 AM

This helps out with 'cat':

http://unixhelp.ed.ac.uk/CGI/man-cgi?cat

As usual, there are several ways to achieve your goals when scripting. Basically what 'echo' does is repeat what you give it as a parameter. So 'echo hello' prints 'hello'. That means you'll need one echo per one line (in the example script) to produce the lines you want. cat on the other hand concatenates things, and if you feed something to it (say a file, which can also be stdin which is standard input, meaning your keyboard!), it will read the contents and print them out (several files cat'ed result in their contents "spit out" after each other, concatenating the whole content). So instead of issuing seven to seven hundred echo commands, one per line, you can use one 'cat' command and feed it data from a special file 'stdin' (the standard input, keyboard, remember?) which it then reads (until the EOF is found, like in the example, so it won't read forever!), and when it finishes, it spits out the same content as echo but with one 'cat' run instead of several 'echo's. The script, when run, then basically just writes the whatever content you want to stdout (standard output, usually terminal) which you can then forward to a html file for example.

You can try just
Code:

cat
and then writing something. Whatever you write, it'll read it and print out. That's one way of using cat..another way is then giving it a "real" file (not stdin for example) and so on. You can quit with CTRL-C.

jerf 09-17-2007 11:56 PM

hello again,

i think what i wasnt getting was that cat serves basically two functions; to not only link files or input together, but also to return the input in the form of standard output much like echo. I had read the man pages before but hadnt grasped the "to standard output" part of "Concatenate FILE(s), or standard input,".

thanks for the clarification.:)

chrism01 09-19-2007 03:12 AM

The short version is that 'echo' expects arg(s) that are/look like variables. 'cat' expects to deal with a whole file (or something that looks like one eg here doc).


All times are GMT -5. The time now is 03:30 AM.