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. |
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:
Example B) Code:
Now, with echo however, you could do something like : Code:
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? |
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 |
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.:) |
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. |